当前位置:   article > 正文

[Android]App之间发送和接收广播_android 发送广播

android 发送广播

这里使用不同模块来实现app之间的广播通信,以两个app为例,通过点击按钮来实现的~

 

1. broad模块 - 忽略BootCompleteReceiver和MainActivity(这是其他的~)

 步骤

(1)创建 BroadOrderActivity,表示发送广播者

 

 

点击右下角的finish即完成创建

(BroadOrderActivity.java和在layout下的activity_broad_order.xml

(2) 书写代码

 这部分代码如下:

BroadOrderActivity.java

  1. package com.example.broad;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener {
  8. private BroadOrderReceiver broadOrderReceiver;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_broad_order);
  13. findViewById(R.id.btn_send_a).setOnClickListener(this);
  14. }
  15. @Override
  16. public void onClick(View view) {
  17. Intent intent = new Intent("com.example.broad.BroadOrderReceiver");
  18. sendOrderedBroadcast(intent,"com.example.broad.BroadOrderReceiver");
  19. }
  20. @Override
  21. protected void onStart() {
  22. super.onStart();
  23. broadOrderReceiver = new BroadOrderReceiver();
  24. IntentFilter filter = new IntentFilter("com.example.broad.BroadOrderReceiver");
  25. registerReceiver(broadOrderReceiver,filter);
  26. }
  27. @Override
  28. protected void onStop() {
  29. super.onStop();
  30. unregisterReceiver(broadOrderReceiver);
  31. }
  32. }

activity_broad_order.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <Button
  8. android:id="@+id/btn_send_a"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="发送广播"/>
  12. </LinearLayout>

(3)在同一个模块下创建一个接收者 BroadOrderReceiver

 

同样点击右下角的finish完成创建 

(4)接收者代码如下(在控制台打印消息表示接收到~):

BroadOrderReceiver.java

  1. package com.example.broad;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class BroadOrderReceiver extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.d("nn","BroadOrderAReceiver接收到广播");
  10. }
  11. }

(5)重要的部分!我自己在这里纠结了好久,终于发现是这里的问题,才不能完成app之间通信

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
  3. <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
  4. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  5. <permission
  6. android:name="com.example.broad.BroadOrderReceiver"
  7. android:protectionLevel="signature"
  8. ></permission>
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@mipmap/ic_launcher"
  12. android:label="@string/app_name"
  13. android:supportsRtl="true"
  14. android:theme="@style/Theme.RecycleView">
  15. <receiver
  16. android:name=".BroadOrderReceiver"
  17. android:enabled="true"
  18. android:exported="true">
  19. <intent-filter>
  20. <action android:name="com.example.broad.BroadOrderReceiver"/>
  21. </intent-filter>
  22. </receiver>
  23. <activity
  24. android:name=".MainActivity"
  25. android:exported="false" />
  26. </receiver>
  27. <activity
  28. android:name=".BroadOrderActivity"
  29. android:exported="true">
  30. <intent-filter>
  31. <action android:name="android.intent.action.MAIN" />
  32. <category android:name="android.intent.category.LAUNCHER" />
  33. </intent-filter>
  34. </activity>
  35. </application>
  36. </manifest>

小小提一下,主要是这里要设置用户的权限(完整代码在上面,这里就是记录一下下):

  1. <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
  2.  <permission
  3.         android:name="com.example.broad.BroadOrderReceiver"
  4.         android:protectionLevel="signature"
  5.         ></permission>

2. broadb模块 - 在我的理解中,就是另一个app

(1)创建模块

 

 点击finish就完成模块的创建

(会自动生成MainActivity.java 和 activity_main.xml,只修改了MainActivity.java)

代码如下:

MainActivity.java

  1. package com.example.broadb;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.IntentFilter;
  4. import android.os.Bundle;
  5. public class MainActivity extends AppCompatActivity {
  6. private BroadcastBReceiver broadcastBReceiver;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. }
  12. @Override
  13. protected void onStart() {
  14. super.onStart();
  15. broadcastBReceiver = new BroadcastBReceiver();
  16. IntentFilter intentFilter = new IntentFilter("com.example.broad.BroadOrderReceiver");
  17. registerReceiver(broadcastBReceiver,intentFilter);
  18. }
  19. @Override
  20. protected void onStop() {
  21. super.onStop();
  22. unregisterReceiver(broadcastBReceiver);
  23. }
  24. }

 (2)创建 BroadcastBReceiver

创建上一个模块一样~

这里就直接附上代码

BroadcastBReceiver.java

  1. package com.example.broadb;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class BroadcastBReceiver extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Log.d("nn","BroadcastBReceiver收到广播");
  10. }
  11. }

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
  3. <uses-permission android:name="com.example.broad.BroadOrderReceiver"/>
  4. <permission android:name="com.example.broad.BroadOrderReceiver"></permission>
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/Theme.RecycleView">
  11. <receiver
  12. android:name=".BroadcastBReceiver"
  13. android:enabled="true"
  14. android:exported="true">
  15. <intent-filter android:priority="100">
  16. <action android:name="com.example.broad.BroadOrderReceiver"/>
  17. </intent-filter>
  18. </receiver>
  19. <activity
  20. android:name=".MainActivity"
  21. android:exported="true">
  22. <intent-filter>
  23. <action android:name="android.intent.action.MAIN" />
  24. <category android:name="android.intent.category.LAUNCHER" />
  25. </intent-filter>
  26. </activity>
  27. </application>
  28. </manifest>

3. 结果 - 成功~

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/122629?site
推荐阅读
相关标签
  

闽ICP备14008679号