这里使用不同模块来实现app之间的广播通信,以两个app为例,通过点击按钮来实现的~
1. broad模块 - 忽略BootCompleteReceiver和MainActivity(这是其他的~)
步骤:
(1)创建 BroadOrderActivity,表示发送广播者
点击右下角的finish即完成创建
(BroadOrderActivity.java和在layout下的activity_broad_order.xml)
(2) 书写代码
这部分代码如下:
BroadOrderActivity.java
package com.example.broad;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener { private BroadOrderReceiver broadOrderReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_broad_order); findViewById(R.id.btn_send_a).setOnClickListener(this); } @Override public void onClick(View view) { Intent intent = new Intent("com.example.broad.BroadOrderReceiver"); sendOrderedBroadcast(intent,"com.example.broad.BroadOrderReceiver"); } @Override protected void onStart() { super.onStart(); broadOrderReceiver = new BroadOrderReceiver(); IntentFilter filter = new IntentFilter("com.example.broad.BroadOrderReceiver"); registerReceiver(broadOrderReceiver,filter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(broadOrderReceiver); }}
activity_broad_order.xml
(3)在同一个模块下创建一个接收者 BroadOrderReceiver
同样点击右下角的finish完成创建
(4)接收者代码如下(在控制台打印消息表示接收到~):
BroadOrderReceiver.java
package com.example.broad;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadOrderReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("nn","BroadOrderAReceiver接收到广播"); }}
(5)重要的部分!我自己在这里纠结了好久,终于发现是这里的问题,才不能完成app之间通信
AndroidManifest.xml
小小提一下,主要是这里要设置用户的权限(完整代码在上面,这里就是记录一下下):
2. broadb模块 - 在我的理解中,就是另一个app
(1)创建模块
点击finish就完成模块的创建
(会自动生成MainActivity.java 和 activity_main.xml,只修改了MainActivity.java)
代码如下:
MainActivity.java
package com.example.broadb;import androidx.appcompat.app.AppCompatActivity;import android.content.IntentFilter;import android.os.Bundle;public class MainActivity extends AppCompatActivity { private BroadcastBReceiver broadcastBReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); broadcastBReceiver = new BroadcastBReceiver(); IntentFilter intentFilter = new IntentFilter("com.example.broad.BroadOrderReceiver"); registerReceiver(broadcastBReceiver,intentFilter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(broadcastBReceiver); }}
(2)创建 BroadcastBReceiver
创建上一个模块一样~
这里就直接附上代码
BroadcastBReceiver.java
package com.example.broadb;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BroadcastBReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("nn","BroadcastBReceiver收到广播"); }}
AndroidManifest.xml
3. 结果 - 成功~
来源地址:https://blog.csdn.net/bellnai/article/details/131140151