Android BroadcastReceiver实例Demo(有序广播的发送)
来源:互联网 发布:日系进口厨刀知多少 编辑:程序博客网 时间:2024/05/16 04:44
上一篇简单介绍了广播的发送,这篇主要介绍下,有序广播的发送。
设置完相关属性的时候,广播就会按照有序的方式进行发送:
发送顺序:
先发送第二条广播;
再发送第一条广播;
最后发送第三条广播。
代码如下:
布局文件:
activity_main(一个Button):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="点击发送有序广播" /></RelativeLayout>
MainActivity:
package com.android_broadcasereceiverdemo2;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction("myaction");intent.putExtra("name", "梅西");sendOrderedBroadcast(intent, null);}});}}
package com.android_broadcasereceiverdemo2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class FirstBroadcast extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String name = intent.getStringExtra("name");Toast.makeText(context, "第一条广播已发送..."+name, Toast.LENGTH_SHORT).show();//abortBroadcast();//当加上这条代码的时候,广播发送到此结束,即第三条广播不会再收到。}}
SecondBroadcast:
package com.android_broadcasereceiverdemo2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class SecondBroadcast extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String name = intent.getStringExtra("name");Toast.makeText(context, "第二条广播已发送..."+name, Toast.LENGTH_SHORT).show();}}
ThirdBroadcast:
package com.android_broadcasereceiverdemo2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class ThirdBroadcast extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String name = intent.getStringExtra("name");Toast.makeText(context, "第三条广播已发送..."+name, Toast.LENGTH_SHORT).show();}}
AndroidManifest.xml(很关键):
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android_broadcasereceiverdemo2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android_broadcasereceiverdemo2.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".FirstBroadcast" > <!-- 有序广播中,priority数值越大,优先级越大,也就越先发送!! --> <intent-filter android:priority="99"> <action android:name="myaction" /> </intent-filter> </receiver> <receiver android:name=".SecondBroadcast" > <intent-filter android:priority="100"> <action android:name="myaction" /> </intent-filter> </receiver> <receiver android:name=".ThirdBroadcast" > <intent-filter android:priority="98"> <action android:name="myaction" /> </intent-filter> </receiver> </application></manifest>
需要源码的读者可以到我的资源中下载。
4 0
- Android BroadcastReceiver实例Demo(有序广播的发送)
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- Android 四大组件之BroadcastReceiver(发送有序无序广播)
- android-BroadcastReceiver有序广播
- Android的Service与BroadcastReceiver之四 BroadcastReceiver 简介、开发与配置;发送广播、有序广播;使用BroadcastReceiver接受系统广播
- Android--广播接收者(发送有序广播)
- Android--有序广播的实例
- 发送有序广播——BroadcastReceiver
- Android 发送有序广播
- Android-发送有序广播
- Android 发送有序广播
- BroadcastReceiver(有序广播篇)
- BroadcastReceiver(有序广播篇)
- Android--有序广播的实例解析--案例《拦截有序广播》
- cocos2dx 把log输出到命令窗口
- MySql避免重复插入记录的几种方法
- 强制卸载LINUX系统自带JDK
- Rss订阅其它网站的文章
- zend studio 改变代码编码格式
- Android BroadcastReceiver实例Demo(有序广播的发送)
- linux下ACE 6.2编译
- ubuntu中删除显示为deinstall的linux内核
- 超实用的8个Linux命令行性能监测工具
- hdu 1255 覆盖的面积
- LeetCode 题目总结
- Javascript基础(一)
- 将数字转换成中文金额的大写形式
- snprintf sscanf