学习郭老师的《第一行代码》进行到广播章节,在5.3中发送自定义广播 5.3.1发送标准广播,按照教材内容敲完,点击按钮后始终看不到吐司的提示,但是动态注册的接收器是可以接到到的。
网上搜了不少内容,包括《关于静态注册BroadcastReceiver接收不到广播的问题》设置标 Intent.FLAG_INCLUDE_STOPPED_PACKAGES也未解决。但阅读过程中却给我启发:想到郭老师当时编教材时环境是 Android 7.0,我现在的开发环境中安装的是 Android 8.0和 Android 9.0版本,猜测谷歌是不是又增加了限制的缘故,才导致的收不到呢?最后通过查看官方的帮助文档果然找到了问题所在,8.0之后仅在清单中声明接收器是不行的。
简单介绍一下代码,包---new--other-- Broadcast Receiver,接收器内代码
1 package com.example.mybroadcast; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.view.Gravity; 7 import android.widget.Toast; 8 9 public class MyReceiver extends BroadcastReceiver { 10 public MyReceiver(){ 11 //自己手动加的构造函数 12 } 13 14 @Override 15 public void onReceive(Context context, Intent intent) { 16 // TODO: This method is called when the BroadcastReceiver is receiving 17 // an Intent broadcast. 感谢panhouye,学到了控制显示格式控制 https://www.cnblogs.com/panhouye/p/6168156.html 18 Toast t=Toast.makeText(context,"静态注册:"+intent.getStringExtra("info"),Toast.LENGTH_SHORT); 19 t.setGravity(Gravity.TOP,0,0); 20 t.show(); 21 } 22 }
AndroidManifest.xml文件内容
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.mybroadcast"> 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:roundIcon="@mipmap/ic_launcher_round" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"> 12 <intent-filter> 13 <action android:name="android.intent.action.MAIN" /> 14 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 <receiver 19 android:name=".MyReceiver" 20 android:enabled="true" 21 android:exported="true"> 22 <intent-filter> 23 <action android:name="WYH"/> 24 </intent-filter> 25 </receiver> 26 </application> 27 28 </manifest>
主活动文件,我的动态注册和静态注册在一起做的练习,所以MainActivity.java文件中能看到相关代码
1 package com.example.mybroadcast; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 public class MainActivity extends AppCompatActivity { 14 15 private IntentFilter intentFilter; 16 private myBroadcastReceiver myBroadcastReceiver; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 //动态注册 23 intentFilter=new IntentFilter(); 24 intentFilter.addAction("MY_BROADCAST"); 25 myBroadcastReceiver=new myBroadcastReceiver(); 26 registerReceiver(myBroadcastReceiver,intentFilter); 27 28 Button button=findViewById(R.id.btnSB); 29 button.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View v) { 32 Intent intent=new Intent(); 33 intent.setAction("WYH"); 34 intent.putExtra("info","油菜花开"); 35 intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 36 intent.setPackage("com.example.mybroadcast"); 37 sendBroadcast(intent); 38 // intent.setAction("MY_BROADCAST"); 39 // sendBroadcast(intent); 40 } 41 }); 42 } 43 44 @Override 45 protected void onDestroy() { 46 super.onDestroy(); 47 unregisterReceiver(myBroadcastReceiver); 48 } 49 50 public class myBroadcastReceiver extends BroadcastReceiver{ 51 @Override 52 public void onReceive(Context context, Intent intent) { 53 Toast.makeText(context,"动态注册"+intent.getStringExtra("info"),Toast.LENGTH_LONG).show(); 54 } 55 } 56 }
主活动布局文件
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 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/btnSB" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:text="Send Broadcast"/> 15 16 </LinearLayout>
在主活动文件加入第36行代码之前,接收器是收不到我发出的广播的,即静态注册接收器没有成功。
通过阅读官方帮助文档后才找到问题所在,如果您的应用面向Android 8.0或更高版本,则无法使用清单为大多数隐式广播(广播不专门针对您的应用)声明接收器,谷歌的考虑应该是避免程序之间打打扰吧
所以我的临时解决方法就是声明我发送的这个intent是专门针对自己应用的 intent.setPackage("com.example.mybroadcast"); 接收器才得以正确接收到广播: