Android Ordered Broadcast 有序广播
来源:互联网 发布:英美留学对比 知乎 编辑:程序博客网 时间:2024/05/17 09:35
代码设置IntentFilter:
IntentFilter intentFilter = new IntentFilter(); intentFilter.setPriority(15); intentFilter.addAction("shortcut.song.com.myapplication.MY_BROADCAST"); intentFilter.addCategory("android.intent.category.LAUNCHER");
MyBroadcast .java
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.Toast;/** * Created by Administrator on 2017/8/16 0016. */public class MyBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "接收到的Intent Action为:"+intent.getAction() + " \n 消息内容:"+intent.getStringExtra("msg") , Toast.LENGTH_SHORT).show(); Bundle bundle = new Bundle(); bundle.putString("first", "Fisrt BroadcastReceiver set message."); // 将bnudle放入结果中 setResultExtras(bundle); // 取消Broadcast的继续传播 //abortBroadcast(); }}
MyBroadcastReceiver2 .java
package shortcut.song.com.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.Toast;/** * Created by Administrator on 2017/8/17 0017. */public class MyBroadcastReceiver2 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); // 解析前一个BroadcastReciver所存入的key为fisrt的消息 String first = bundle.getString("first"); Toast.makeText(context,"上一个Broadcast存入的消息为:" + first, Toast.LENGTH_SHORT).show(); }}
AndroidManifest.xml
<receiver android:name=".MyBroadcast"> <intent-filter android:priority="20"> <action android:name="shortcut.song.com.myapplication.MY_BROADCAST" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiver2"> <intent-filter android:priority="0"> <action android:name="shortcut.song.com.myapplication.MY_BROADCAST" /> </intent-filter> </receiver>
package shortcut.song.com.myapplication;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class OrderedBroadcastActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ordered_broadcast); } public void sendOrderedBroadCast(View v) { Intent intent = new Intent(); intent.setAction("shortcut.song.com.myapplication.MY_BROADCAST"); intent.putExtra("msg", "First message."); // 发送有序广播 sendOrderedBroadcast(intent, null); }}
运行效果:
阅读全文
0 0
- Android Ordered Broadcast 有序广播
- Ordered Broadcast有序广播
- Android 为什么要设立有序广播 Ordered Broadcast
- APP开发实战157-Ordered broadcast(有序广播)简介
- Broadcast详解:有序广播
- 有序广播Ordered broadcasts的使用
- Android四大基本组件之一 ,广播(broadcast receiver)---有序广播
- android--broadcast(广播)
- Android 广播机制---BroadCast
- Android 广播机制 Broadcast
- Android 广播 Broadcast
- Android之Broadcast(广播)
- Android - Intent广播(broadcast)
- android广播机制Broadcast
- Android 广播BroadCast
- android--广播Broadcast【十三】
- android Broadcast广播
- android广播broadcast详解
- asp.net apk文件不能直接下载的解决方法
- Gym
- 简单Python决策树可视化实例
- 体验一个全新的RTOS-QNX系统功能介绍
- 关于Java Web 使用Java的TimerTask, 触发定时器(执行某些任务)的实例
- Android Ordered Broadcast 有序广播
- Nginx 常用负载均衡配置方式
- NS3基本知识
- idea中添加SVN路径
- eclipse安装svn插件
- HDU 6080 度度熊保护村庄(计算几何+最小环-Floyd)
- Java程序通过代理访问网络
- 验证哥德巴赫猜想
- Hadoop集群的各部分的默认端口