赞
踩
Android中常用的跨进程通信方法有以下几种:
Intent、Binder、AIDL、Messenger、ContentProvider。
可以通过Intent传递数据和消息,但是只能传递一些简单的数据类型,比如字符串、整数等。
示例:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MainActivity"));
intent.putExtra("key", "value");
startActivity(intent);
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(serviceConnection);
Bundle bundle = new Bundle();
bundle.putParcelable("key", myObject);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MyService"));
intent.putExtras(bundle);
sendBroadcast(intent);
是Android系统中的跨进程通信机制,它可以传递任意类型的对象,包括自定义类的对象。
示例:
public class MyService extends Service {
private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
@Override
public String getMessage() throws RemoteException {
return "Hello from MyService";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
String message = myAidlInterface.getMessage();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);
是Android系统中的一种接口描述语言,用于定义服务端和客户端之间的接口。
示例同上。
是一种轻量级的通信方式,可以在不同的进程之间传递消息。
示例:
public class MyService extends Service { private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override public String getMessage() throws RemoteException { return "Hello from MyService"; } }; private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { Bundle bundle = msg.getData(); String message = bundle.getString("message"); Log.d(TAG, "Received message: " + message); } }; @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } }
IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
Message message = Message.obtain(null, 0, null, myAidlInterface.getMessage());
myService.mHandler.sendMessage(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);
是一种特殊的IContentObserver,用于在不同的应用程序之间共享数据。
以下是一个简单的示例:在主进程中创建一个ContentProvider,然后在子进程中通过ContentResolver访问这个ContentProvider来获取数据。
// 在主进程中注册ContentProvider public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ContentProvider myContentProvider = new MyContentProvider(); registerContentProvider(myContentProvider); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub return null; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } }
// 在子进程中通过ContentResolver访问ContentProvider获取数据 public class MyActivity extends Activity { private static final String AUTHORITY = "com.example.myapp.provider"; private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Cursor cursor = getContentResolver().query(BASE_CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { // TODO: Do something with the data in the cursor. } cursor.close(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。