发送自定义广播

广播分为标准广播与有序广播

标准广播:

新建一个类MyBroadcastReceiver()使其继承BroadCastReceive并重写方法onReceive()

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"receive in myBroadcastReceive",Toast.LENGTH_SHORT).show();
    }
}

当收到自定义广播时,会弹出Toast中的内容

然后在AndroidManifest中对广播接收器进行修改

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

    </application>

由以上代码可知,让MyBroadcastReceiver接收一条com.example.broadcasttest.MY_BROADTEST的广播,因此在发送广播时我们就应该发送一条这样的广播

接下来我们在activity_main.xml中添加一个按钮作为发送广播的枢纽

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.arturia.broadcasetest.MainActivity">
    
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送广播"/>

</android.support.constraint.ConstraintLayout>

接下来修改逻辑代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonl=(Button)findViewById(R.id.b1);
        buttonl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent=new Intent("com.example.broadcasttest.MY_BROADTEST");
              sendOrderedBroadcast(intent,null);
     }
        });
    }

好了,这就是这就是一条标准广播

有序广播:

一种同步执行的广播,同一时刻只会有一个广播接收器收到广播消息,当这个接收器在逻辑执行完毕后,广播才会继续传递,此时的接收器是有先后顺序的,优先级高的会先执行,只有优先级的设置,下面会说到。并且前面的广播接收器可以截断正在传递的广播,这样后面的接收器就不会收到广播消息了。

新建一个项目BroadcastTest2,一标准广播相同,再重新定义一个类AnotherBoradcastReceiver

public class AnotherBoradcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"receive in AnothermyBroadcastReceive",Toast.LENGTH_SHORT).show();
        abortBroadcast();
    }
}
内容与第一个相同,我们就不做解释了

然后我们设置AndroidManifest.xml中的代码,让他继续接收com.example.broadcasttest.MY_BROADTEST广播

 <receiver
            android:name=".AnotherBoradcastReceiver"
            android:enabled="true"
            android:exported="true"
            >
        <intent-filter android:priority="100">
            <action android:name="com.example.broadcasttest.MY_BROADTEST"></action>
        </intent-filter>>
            </receiver>

运行BroadcastTest2程序,然后再重新回到BroadcastTest项目主界面,点击send,我们会发现,他会连续弹出两个Toast。

这就说明,我们的应用程序发出广播是具有可跨程序性的

但是以上的代码依然是标准广播,下面我们写一下有序广播,我们设置AnotherBoradcastReceiver的优先级,并将广播在onreceive中截断

有序广播需要改一行代码:将sendBroadcast()方法改为senOrderedBroadcast()方法,此方法接收两个参数,一个为intent,第二个为与权限有关的字符串,我们传入null

 Intent intent=new Intent("com.example.broadcasttest.MY_BROADTEST");
        sendOrderedBroadcast(intent,null);

然后修改AndroidManifest.xml中的代码将优先级设为100(使用android.priority)

  <receiver
            android:name=".AnotherBoradcastReceiver"
            android:enabled="true"
            android:exported="true"
            >
        <intent-filter android:priority="100">
            <action android:name="com.example.broadcasttest.MY_BROADTEST"></action>
        </intent-filter>>
            </receiver>

最后将关闭截断,使其不再继续传递

public class AnotherBoradcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"receive in AnothermyBroadcastReceive",Toast.LENGTH_SHORT).show();
        abortBroadcast();
    }
}

在onReceive()方法中调用abortBroadcast()方法,这样就ok了

运行程序我们会发现只会有一条"receive in AnothermyBroadcastReceive"的toast

好了,标准广播和本地广播并不难理解

@.@

 

猜你喜欢

转载自blog.csdn.net/M_Edison/article/details/82746681
今日推荐