Android學習之坑——靜態廣播問題

 

 

一、前言

       因爲工作的需要吧,之前都是研究Unity的,現在需要研究Android,於是就找了本經典的書《第一行代碼》(第二版)看。看到第五章的時候就遇到坑了,發現按照書上的寫代碼根本運行得不到書上的效果,我使用的是Android Studio3.5.1 SDK是Version29。其中5.3節講到需要一個廣播得接收者來接收發送自定義得廣播,並且其他的APP中也可以接收到這個廣播。然而按照書上的去做,我不但其他APP上沒接收到發送APP上的的消息,就連自身APP上的廣播接收者也沒接收到。

二 、具體解決無法接收廣播的問題

1、同一個APP中無法接收廣播的問題

解決這個問題需要注意兩個地方即可,一個是”AndroidManifest.xml“的註冊XML代碼,另外一個是發送時候的代碼。

1.1、註冊代碼”AndroidManifest.xml“中的注意事項

書中在同一個APP中定義了“MyBroadcastReceiver”,然後再該類中就寫了一個提示程序。代碼如下:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_
            SHORT).show();
    }

}

這一步都是沒有問題,接下來在“AndroidManifest.xml”中的註冊就不對了,書中的註冊XML代碼爲:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest">
    ...
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

要將其中的代碼: <action android:name="com.example.broadcasttest.MY_BROADCAST"/>

改成:<action android:name="com.example.boradcasttestapplication.MyBroadcastReceiver"/>    

也就是這裏註冊的名字必須要符合如下規範

<action android:name="com.example.包的名字.剛定義接收廣播的類("MyBroadcastReceiver“/>

1.2、發送代碼的注意事項

書中的在發送代碼:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new 
                Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });

將按鈕裏的處理代碼修改爲如下代碼:

                Intent intent = new Intent("com.example.boradcasttestapplication.MyBroadcast");
                intent.setComponent(new ComponentName("com.example.boradcasttestapplication", "com.example.boradcasttestapplication.MyBroadcast"));
                sendBroadcast(intent);

2、發送APP在另外一個APP無法接收的問題

書上的接收的APP程序沒有什麼問題,還是在靜態註冊的”AndroidManifest.xml“文件中,將書中的代碼片段

        <receiver
            android:name=".AnotherBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST" />
            </intent-filter>
        </receiver>

中的 <action android:name="com.example.broadcasttest.MY_BROADCAST" /> 替換成

 <action android:name="com.example.第二個APP程序名.接收類名" />,比如我自己寫的第二個APP程序”mybroadcastreceiverapplication2“,其中定義了一個接收廣播的類“AnotherBroadcastReceiver“,因此我在”AndroidManifest.xml“文件中的代碼就爲:

        <receiver
            android:name=".AnotherBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.mybroadcastreceiverapplication2.AnotherBroadcastReceiver"/>
            </intent-filter>
        </receiver>

最後一步:在發送的APP中,單獨在寫一個發送的代碼,而不是書上的一個發送代碼。也即除了第一個APP上的發送不變,還需要再寫一個專門發送給第二個APP的代碼。

                Intent intent = new Intent("com.example.boradcasttestapplication.MyBroadcast");
                intent.setComponent(new ComponentName("com.example.boradcasttestapplication", "com.example.boradcasttestapplication.MyBroadcast"));
                sendBroadcast(intent);

                Intent intent2=new Intent("com.example.mybroadcastreceiverapplication2.AnotherBroadcastReceiver");
                intent2.setComponent(new ComponentName("com.example.mybroadcastreceiverapplication2","com.example.mybroadcastreceiverapplication2.AnotherBroadcastReceiver"));
                sendBroadcast(intent2);

三、總結

1、書上的代碼可能是因爲API版本已經過時的原因導致廣播不能接收或發送成功

2、同一個APP下要注意在“AndroidManifest.xml”註冊的廣播接收者的名字一定要和定義的類名字一致,另一個APP中的“AndroidManifest.xml”也是一樣,一定要保證在XML文件中註冊的名字(包括包名)要和該類一致

3、發送給另外一個APP的代碼中要單獨在寫一個發送的,匹配的標識要和第二個APP的接收類一致

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章