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的接收类一致

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