当前位置:实例文章 » JAVA Web实例» [文章]Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信

Android手写占位式插件化框架之Activity通信、Service通信和BroadcastReceiver通信

发布人:shili8 发布时间:2024-12-24 07:25 阅读次数:0

**Android 手写占位式插件化框架之 Activity 通信、Service 通信和 BroadcastReceiver 通信**

在 Android 应用开发中,插件化框架是为了实现应用的模块化设计和扩展性而生的。手写占位式插件化框架是一种基于 Android 的插件化解决方案,它允许开发者通过编程方式来定义和管理插件之间的通信关系。

在本文中,我们将重点讨论 Activity 通信、Service 通信和 BroadcastReceiver 通信这三个方面的实现原理和代码示例。

**一、Activity 通信**

Activity 通信是指两个或多个 Activity 之间通过编程方式来交换数据和事件的过程。手写占位式插件化框架提供了以下几种方式来实现 Activity 通信:

###1.1 Intent 方式Intent 是 Android 中一个非常重要的组件,它可以用来传递数据、启动 Activity 或 Service 等。

java// 发送 Intentpublic class MainActivity extends AppCompatActivity {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 Intent intent = new Intent(ACTION_SEND_DATA);
 intent.putExtra("key", "value");
 sendBroadcast(intent);
 }
}

// 接收 Intentpublic class SecondActivity extends AppCompatActivity {
 private BroadcastReceiver receiver;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 String value = intent.getStringExtra("key");
 Log.d("SecondActivity", "Received data: " + value);
 }
 };
 IntentFilter filter = new IntentFilter(MainActivity.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 }
}


###1.2 Binder 方式Binder 是 Android 中一个用于进程间通信的机制,它可以用来传递数据和事件。

java// 发送 Binderpublic class MainActivity extends AppCompatActivity {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 IBinder binder = new Binder();
 Intent intent = new Intent(ACTION_SEND_DATA);
 intent.putExtra("binder", binder);
 sendBroadcast(intent);
 }
}

// 接收 Binderpublic class SecondActivity extends AppCompatActivity {
 private BroadcastReceiver receiver;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 IBinder binder = (IBinder) intent.getParcelableExtra("binder");
 Log.d("SecondActivity", "Received Binder: " + binder);
 }
 };
 IntentFilter filter = new IntentFilter(MainActivity.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 }
}


###1.3 ContentProvider 方式ContentProvider 是 Android 中一个用于共享数据的机制,它可以用来传递数据和事件。

java// 发送 ContentProviderpublic class MainActivity extends AppCompatActivity {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 ContentValues values = new ContentValues();
 values.put("key", "value");
 getContentResolver().insert(Uri.parse(ACTION_SEND_DATA), values);
 }
}

// 接收 ContentProviderpublic class SecondActivity extends AppCompatActivity {
 private BroadcastReceiver receiver;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // ...
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 Uri uri = intent.getData();
 Cursor cursor = getContentResolver().query(uri, null, null, null, null);
 if (cursor != null && cursor.moveToFirst()) {
 String value = cursor.getString(cursor.getColumnIndex("key"));
 Log.d("SecondActivity", "Received data: " + value);
 }
 }
 };
 IntentFilter filter = new IntentFilter(MainActivity.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 }
}


**二、Service 通信**

Service 通信是指两个或多个 Service 之间通过编程方式来交换数据和事件的过程。手写占位式插件化框架提供了以下几种方式来实现 Service 通信:

###2.1 Binder 方式Binder 是 Android 中一个用于进程间通信的机制,它可以用来传递数据和事件。

java// 发送 Binderpublic class MyService extends Service {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override public IBinder onBind(Intent intent) {
 return new Binder();
 }
}

// 接收 Binderpublic class SecondService extends Service {
 private BroadcastReceiver receiver;
 @Override public void onCreate() {
 super.onCreate();
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 IBinder binder = (IBinder) intent.getParcelableExtra("binder");
 Log.d("SecondService", "Received Binder: " + binder);
 }
 };
 }
 @Override public int onStartCommand(Intent intent, int flags, int startId) {
 // ...
 IntentFilter filter = new IntentFilter(MyService.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 return super.onStartCommand(intent, flags, startId);
 }
}


###2.2 AIDL 方式AIDL 是 Android 中一个用于定义接口的机制,它可以用来传递数据和事件。

java// 发送 AIDLpublic interface IMyService extends android.os.IInterface {
 void sendData(String data) throws RemoteException;
}

// 接收 AIDLpublic class SecondService extends Service {
 private BroadcastReceiver receiver;
 @Override public void onCreate() {
 super.onCreate();
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 IMyService service = IMyService.Stub.asInterface(intent.getParcelableExtra("service"));
 String data = service.sendData("Hello, World!");
 Log.d("SecondService", "Received data: " + data);
 }
 };
 }
 @Override public int onStartCommand(Intent intent, int flags, int startId) {
 // ...
 IntentFilter filter = new IntentFilter(MyService.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 return super.onStartCommand(intent, flags, startId);
 }
}


###2.3 ContentProvider 方式ContentProvider 是 Android 中一个用于共享数据的机制,它可以用来传递数据和事件。

java// 发送 ContentProviderpublic class MyService extends Service {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override public void onCreate() {
 super.onCreate();
 ContentValues values = new ContentValues();
 values.put("key", "value");
 getContentResolver().insert(Uri.parse(ACTION_SEND_DATA), values);
 }
}

// 接收 ContentProviderpublic class SecondService extends Service {
 private BroadcastReceiver receiver;
 @Override public void onCreate() {
 super.onCreate();
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent) {
 Uri uri = intent.getData();
 Cursor cursor = getContentResolver().query(uri, null, null, null, null);
 if (cursor != null && cursor.moveToFirst()) {
 String value = cursor.getString(cursor.getColumnIndex("key"));
 Log.d("SecondService", "Received data: " + value);
 }
 }
 };
 }
 @Override public int onStartCommand(Intent intent, int flags, int startId) {
 // ...
 IntentFilter filter = new IntentFilter(MyService.ACTION_SEND_DATA);
 registerReceiver(receiver, filter);
 return super.onStartCommand(intent, flags, startId);
 }
}


**三、BroadcastReceiver 通信**

BroadcastReceiver 通信是指两个或多个 BroadcastReceiver 之间通过编程方式来交换数据和事件的过程。手写占位式插件化框架提供了以下几种方式来实现 BroadcastReceiver 通信:

###3.1 Intent 方式Intent 是 Android 中一个非常重要的组件,它可以用来传递数据、启动 Activity 或 Service 等。

java// 发送 Intentpublic class MyReceiver extends BroadcastReceiver {
 public static final String ACTION_SEND_DATA = "com.example.action.send_data";
 @Override public void onReceive(Context context, Intent intent) {
 // ...
 Intent broadcastIntent = new Intent(ACTION_SEND_DATA);
 broadcastIntent.putExtra("key", "value");
 sendBroadcast(broadcastIntent);
 }
}

// 接收 Intentpublic class SecondReceiver extends BroadcastReceiver {
 private BroadcastReceiver receiver;
 @Override public void onReceive(Context context, Intent intent) {
 // ...
 receiver = new BroadcastReceiver() {
 @Override public void onReceive(Context context, Intent intent)

相关标签:android
其他信息

其他资源

Top