广播接收者的使用
来源:互联网 发布:淘宝 论文代发 卓越 编辑:程序博客网 时间:2024/06/05 01:53
静态注册:
1、创建两个类,继承BroadcastReceiver并实现onReceive
public class LaoDa extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String resultData = getResultData();//获取接受到的广播信息 System.out.println(resultData+"钱很多 1"); }}
public class LaoEr extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String resultData = getResultData();//获取接收到的广播信息 System.out.println(resultData+"钱很多 2"); }}
2、在 清单文件AndroidManifest > application 中注册广播receiver <注意:intent-filter后的 priority属性 是设置接收到广播的优先级,值越大优先级越高,这个值的范围为 -1000至1000>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".LaoDa"> <intent-filter android:priority="1000"> <action android:name="QiLin666" /> </intent-filter> </receiver> <receiver android:name=".LaoEr"> <intent-filter android:priority="900"> <action android:name="QiLin666"/> </intent-filter> </receiver></application>
3、在activity中向两个广播发送数据
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("QiLin666");//设置让谁接收 sendOrderedBroadcast(intent,null,null,null,1,"发钱啦,",null);//发送广播 } }); }}
动态注册:
1、与静态注册的第一步相同
2、先动态注册广播,再发送广播
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //动态注册广播接收器 LaoDa laoDa = new LaoDa(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("QiLin666"); registerReceiver(laoDa,intentFilter);LaoEr laoEr = new LaoEr();IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("QiLin666"); registerReceiver(laoEr,intentFilter);
Button mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("QiLin666");//设置让谁接收 sendOrderedBroadcast(intent,null,null,null,1,"毛线",null);//发送广播 } }); }}
这是xml:
好了,这边是广播接收者的简单使用,希望对您有所帮助!<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播" /></RelativeLayout>
2 0
- 广播接收者的使用
- 广播接收者BroadcastReceiver的使用
- 广播接收者的优先级
- 短信的广播接收者
- 特殊的广播接收者
- 限制广播的接收者
- 使用广播接收者监听短信
- 使用广播接收者窃听短信
- Android BroadCastReceiver 广播接收者使用
- 使用服务注册广播接收者
- Android广播接收者使用总结
- Android广播接收者使用总结
- BroadcastReceiverTest用例了解广播接收者BroadcastReceiver的使用
- Adroid中广播接收者的使用,实现强制下线功能
- android服务_使用服务注册特殊的广播接收者
- Android中使用广播接收者来弹出对话框的坑
- 广播接收者常用的action
- Android 广播接收者的生命周期
- 进程环境
- Linux 和 Windows Socket编程的区别
- Ruby 学习 #笨办法学ruby# 习题11 gets和gets.chomp的区别
- Valgrind使用说明
- 三大WEB服务器对比分析
- 广播接收者的使用
- 百度二面面经及总结
- logback 常用配置详解(二)<appender>
- Netty 之 AttributeMap属性
- Sencha Touch开发心得分享
- 关于 23 种设计模式的有趣见解
- eclispe不出log
- 人脸检测——HyperFace
- web局部打印、去页眉页尾、兼容IE、Firefox、Chrome