发送有序广播
2017-06-03 14:19
288 查看
这次演示的是发送有序广播,首先什么是有序广播呢?就是一种同步执行的广播,同一时刻仅只有一个接收器接收到消息!
下面进行简单演示。
创建好基本的包后,进行页面布局,需要一个发送按钮,以及一个自定义背景就行。下面是我的布局。
定义了发送按钮,就要触发功能,实现的功能就是一点击发送,便会发出广播,下面是MainActivity的代码
上面用到了3个接收者,既然有接收者,就需要对其进行添加注册,在manifests进行注册。
注意的是android:priority,这代表的是优先级,开头提过有序广播是有优先级的,值大越优先,那么问题来了,如果俩个优先级一样,谁先接收呢?最后在回答,先看下运行结果。
点击按钮便会发送,接收界面如下
可以看出优先级,那么刚才的问题答案便是,当出现优先级一样的情况时,注册广播接收者早的优先接收,,,是的,就是这样!
下面进行简单演示。
创建好基本的包后,进行页面布局,需要一个发送按钮,以及一个自定义背景就行。下面是我的布局。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.bz0209.myapplication.MainActivity" android:background="@drawable/a" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:onClick="send" android:text="发送有序广播" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#FBFBFF" android:textSize="20sp" /> </RelativeLayout>
定义了发送按钮,就要触发功能,实现的功能就是一点击发送,便会发出广播,下面是MainActivity的代码
package com.example.bz0209.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View view){ Intent intent=new Intent(); intent.setAction("Intercept_Stitch"); sendOrderedBroadcast(intent,null); } public class MyBroadcastReceiverOne extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件"); } } public class MyBroadcastReceiverTwo extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件"); } }public class MyBroadcastReceiverThree extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件"); } } }
上面用到了3个接收者,既然有接收者,就需要对其进行添加注册,在manifests进行注册。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bz0209.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".MainActivity$MyBroadcastReceiverOne"> <intent-filter android:priority="1000" > <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MainActivity$MyBroadcastReceiverTwo"> <intent-filter android:priority="200" > <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MainActivity$MyBroadcastReceiverThree"> <intent-filter android:priority="600" > <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> a4f4 </intent-filter> </activity> </application> </manifest>
注意的是android:priority,这代表的是优先级,开头提过有序广播是有优先级的,值大越优先,那么问题来了,如果俩个优先级一样,谁先接收呢?最后在回答,先看下运行结果。
点击按钮便会发送,接收界面如下
可以看出优先级,那么刚才的问题答案便是,当出现优先级一样的情况时,注册广播接收者早的优先接收,,,是的,就是这样!
相关文章推荐
- 发送有序广播——BroadcastReceiver
- 广播(有序)跨应用发送简单举例
- 发送有序广播
- 通过sendOrderedBroadcast()方法发送一条有序广播
- 发送一条普通广播 有序广播 Broadcast 和 Receiver
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播案例
- android-BroadcastReceiver 发送有序广播
- 发送有序广播案例
- Android--广播接收者(发送有序广播)
- 发送自定义广播 之 有序广播
- 发送有序广播
- 发送有序广播
- Android中发送有序广播案例
- 发送有序广播
- 安卓BroadcastReceiver组件使用系列2:给多个广播接收者发送广播和有序广播的使用
- android 发送有序广播sendOrderBraodCast、截断广播和广播之间的数据传递
- 发送有序广播