學習郭老師的《第一行代碼》進行到廣播章節,在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"); 接收器才得以正確接收到廣播: