android 如何让两个apk相互通信? 如何用eclipse编写两个APP实现网络互相通信

\u5982\u4f55\u8ba9\u4e24\u4e2aandroid\u624b\u8fdb\u884cble\u901a\u4fe1

1\uff0c\u754c\u9762\u4e0a\u53ea\u6709\u4e00\u4e2a\u6309\u94ae\u548c\u4e00\u4e2alistview

[html] view plain copy

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
>
<Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u641c\u7d22"
android:layout_marginTop="40dp"
android:layout_marginLeft="40dp"
android:background="@null"
android:textColor="#ff2222ff"

\u5927\u6982\u7684\u5199\u4e00\u4e0b\u6b65\u9aa4\u7ed9\u4f60\uff1a
1.\u5728android\u5de5\u7a0b\u7684\u914d\u7f6e\u6587\u4ef6\u91cc\u52a0\u4e0a\u8c03\u7528\u7cfb\u7edf\u77ed\u4fe1\u529f\u80fd\u7684\u6743\u9650\uff1a
2\u3001\u4e3b\u8981\u4ee3\u7801\u7247\u6bb5\uff1a
public class testSms extends Activity {... private void send1(String phone, String message){ PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, testSms.class), 0); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phone, null, message, pi, null); }}\u53c2\u6570\uff1aphone\uff1a\u624b\u673a\u53f7\u7801
message\uff1a\u77ed\u4fe1\u5185\u5bb9

A.apk,B.apk都是我写的.现在我想在B.apk里实现获得A.apk的某控件ID,并向其发送一个按键事件.如何实现,谢谢!也就是用B控制A.

这两个apk 是在两个进程里的,(或许可以配置成一个进程。不过如何直接操作这个没有试过。)

所以一般的有两种方法:
1、RPC
2、通过 BroadcastRecever 或 Service

第二种方法比较简单一些。
比如B 控制 A的话,
A定义Service ,比如com.my.ServerA .自定义actionFilter “com.my.serverA.action"
B中调用Intent it = newIntent("com.my.serverA.action");
it.putExtra(....);// 传递你的自定义控制指令。
B.startService(it);
在A中ServerA的 onStartService(Intent it) 。解析这个控制指令,做相应操作就可以了。

用BroadcastRecever 的道理一样。

Aidl,android平台的IPC方式之一,基于系统的Ibinder机制。
网上大多数例子都是在一个apk下来测试调用service,现在我在两个project下面来调用。
一个是server project,一个是client project
首先我们建立的是server project,这里面要实现aidl文件和一个service,activity只是用来启动service的,当然,你也可以通过发广播的形式来启动service。
首先看IAidlService.aidl文件:

Java代码
package com.ds.server;
interface IAidlService {
int getType();
}

这样在eclipse里面自动编译的时候会在gen下面生成IAidlService.java文件(灯下我们的client project要用)。
然后新建一个service,这个service里面has a IAidlService的stub对象,service具体代码如下:

Java代码
package com.ds.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class AidlService extends Service {

private IAidlService.Stub mBinder = new IAidlService.Stub() {

@Override
public int getType() throws RemoteException {
// TODO Auto-generated method stub
return 5;
}
};

private void Log(String str) {
Log.d("AidlService", "------ " + str + "------");
}

@Override
public void onCreate() {
Log("service create");
}

@Override
public void onStart(Intent intent, int startId) {
Log("service start id=" + startId);
}

@Override
public IBinder onBind(Intent t) {
Log("service on bind");
return mBinder;
}

@Override
public void onDestroy() {
Log("service on destroy");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
Log("service on unbind");
return super.onUnbind(intent);
}

public void onRebind(Intent intent) {
Log("service on rebind");
super.onRebind(intent);
}

}

这里一定要实现onBind方法,并返回一个IAidlService.Stub对象。
再去AndroidManifest.xml注册这个service:

Xml代码
<service
android:name=".AidlService"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.ds.server.IAidlService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>

android:enabled="true"
android:process=":remote"这两个标签可有可无。
只要注册了这个service就行。
好了,到此,服务端已经完成。

-------------------华丽的分割线-----------------------------------------

下面我们开始client project。
client project比较简单,需要注意的地方是,首先需要把server project中gen文件夹中aidl生成的那个IAidlService.java类以及包都拷贝到我们的client project中。
(注意:client project的包名为com.ds.client;另外一个包名com.ds.server以及这个server包下面的IAidlService.java类都是从server project的gen文件夹拷贝过来的,至于gen文件夹的其他文件就不需要拷贝过来。)。
好了,这样的话,client project只要从activity去远程调用service就好了,实现代码如下:

Java代码
package com.ds.client;

import com.ds.server.IAidlService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AidlClientActivity extends Activity {

IAidlService iservice;

private ServiceConnection connection = new ServiceConnection() {

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
// 从远程service中获得AIDL实例化对象
iservice = IAidlService.Stub.asInterface(service);
Log.i("Client","Bind Success:" + iservice);
}

public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
iservice = null;
Log.i("Client","onServiceDisconnected");
}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = (TextView) findViewById(R.id.tv);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent service = new Intent(IAidlService.class.getName());
bindService(service, connection, BIND_AUTO_CREATE);
if (iservice != null) {
try {
tv.setText("" + iservice.getType());
} catch (RemoteException e) {
e.printStackTrace();
}
}

}

});

}
}

注意几点:
1,import com.ds.server.IAidlService;使用的是我们拷贝过来的IAidlService.java类
2,需要一个ServiceConnection对象
3,通过Intent service = new Intent(IAidlService.class.getName());
bindService(service, connection, BIND_AUTO_CREATE);来bind service。这样就可以调用aidl中定义的接口来获取service中的值了。

唉,由于在使用中没有注意拷贝server project中gen文件夹下面的包和IAidlService.java,老是出现Unable to start service Intent这样的错误。搞了好久。
附件是源码。注意使用的时候,先要运行server project,启动服务,然后再运行client project。

扩展阅读:android苹果版下载 ... android同时播放两个视频 ... android下载安装app ... android系统怎么打开 ... android开发入门教程 ... androidx869.0无法联网 ... android键盘下载安装 ... android auto中国版破解 ... android怎么互相跳转 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网