Android广播机制
2015-10-29 15:03
525 查看
动态注册系统广播
系统广播不用sendBroadcast()系统自动会发送
定义全局变量 ConnectionChangeReceiver myReceiver;
中注册广播
//静态注册系统广播
发送普通广播
动态发送有序化广播
首先定义一个intentFilter name
PERM_PRIVATE是一个自定义设置广播权限的值
在AndroidManifest中加上自定义权限
发送有序化的广播
广播发送端
广播接收
注册了两个广播接收者 由于广播接收者2的priority很低 那么先是广播接收者1收到广播并响应,1可以设置广播是否让广播继续往下传 setResultCode(Activity.RESULT_CANCELED)
广播2判断这个结果 确定是否响应if (getResultCode() != Activity.RESULT_OK)
广播接收器1//动态接收者
广播接收器2静态注册接收
系统广播不用sendBroadcast()系统自动会发送
定义全局变量 ConnectionChangeReceiver myReceiver;
中注册广播
在oncreate(){ ..... IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); myReceiver=new ConnectionChangeReceiver(); this.registerReceiver(myReceiver, filter); } class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { ConnectivityManager conManager = (ConnectivityManager) getApplicationContext() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = conManager.getActiveNetworkInfo(); if (networkInfo != null) { // 注意,这个判断一定要,要不然会出错 netStatus = networkInfo.isAvailable(); networkErr.setVisibility(View.GONE); }else{ netStatus=false; networkErr.setVisibility(View.VISIBLE); } } } //中取消注册 onDestroy(){ ....... this.unregisterReceiver(myReceiver); //取消监听网络的广播注册 }
//静态注册系统广播
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bignerdranch.android.photogallery" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application ... > <activity ... > ... </activity> <service android:name=".PollService" /> <receiver android:name=".StartupReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest> public class StartupReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Received broadcast intent: " + intent.getAction()); //静态注册的广播 文中广播是监听程序启动完成状态,监听到就会走onReceive()方法 可以在这里加代码开启操作 } }
发送普通广播
首先定义一个广播 intentfilter 名字 public static final String ACTION_SHOW_NOTIFICATION = "com.bignerdranch.android.photogallery.SHOW_NOTIFICATION"; 发送广播sendBroadcast(new Intent(ACTION_SHOW_NOTIFICATION)); //广播接收端 动态自定义注册 public abstract class VisibleFragment extends Fragment { public static final String TAG = "VisibleFragment"; private BroadcastReceiver mOnShowNotification = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getActivity(), "Got a broadcast:" + intent.getAction(), Toast.LENGTH_LONG) .show(); } }; @Override public void onResume() { super.onResume(); IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION); getActivity().registerReceiver(mOnShowNotification, filter); } @Override public void onPause() { super.onPause(); getActivity().unregisterReceiver(mOnShowNotification); } }
动态发送有序化广播
首先定义一个intentFilter name
public static final String ACTION_SHOW_NOTIFICATION = "com.bignerdranch.android.photogallery.SHOW_NOTIFICATION"; public static final String PERM_PRIVATE = "com.bignerdranch.android.photogallery.PRIVATE";
PERM_PRIVATE是一个自定义设置广播权限的值
在AndroidManifest中加上自定义权限
<span style="font-size:14px;"><permission android:name="com.bignerdranch.android.photogallery.PRIVATE" android:protectionLevel="signature" /> //signature代表只能是当前app响应 <uses-permission android:name="com.bignerdranch.android.photogallery.PRIVATE" /></span>
发送有序化的广播
广播发送端
<span style="font-size:12px;">Intent i = new Intent(ACTION_SHOW_NOTIFICATION); i.putExtra("REQUEST_CODE", requestCode); i.putExtra("NOTIFICATION", notification); //Context.sendOrderedBroadcast(Intent,String,BroadcastReceiver,Handler,int,String,Bundle) sendOrderedBroadcast(i, PERM_PRIVATE, null, null, Activity.RESULT_OK, null, null); </span>
广播接收
注册了两个广播接收者 由于广播接收者2的priority很低 那么先是广播接收者1收到广播并响应,1可以设置广播是否让广播继续往下传 setResultCode(Activity.RESULT_CANCELED)
广播2判断这个结果 确定是否响应if (getResultCode() != Activity.RESULT_OK)
广播接收器1//动态接收者
private BroadcastReceiver mOnShowNotification = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // if we receive this, we're visible, so cancel // the notification Log.i(TAG, "canceling notification"); setResultCode(Activity.RESULT_CANCELED); } }; @Override public void onResume() { super.onResume(); IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION); getActivity().registerReceiver(mOnShowNotification, filter, PollService.PERM_PRIVATE, null); } @Override public void onPause() { super.onPause(); getActivity().unregisterReceiver(mOnShowNotification); } }
广播接收器2静态注册接收
android:priority="-999"级别最低 他将最后收到广播 在AndroidManifest中 <receiver android:name=".NotificationReceiver" android:exported="false"> <intent-filter android:priority="-999"> <action android:name="com.bignerdranch.android.photogallery.SHOW_NOTIFICATION" /> public class NotificationReceiver extends BroadcastReceiver { private static final String TAG = "NotificationReceiver"; @Override public void onReceive(Context c, Intent i) { Log.i(TAG, "received result: " + getResultCode()); if (getResultCode() != Activity.RESULT_OK) // a foreground activity cancelled the broadcast return; int requestCode = i.getIntExtra("REQUEST_CODE", 0); Notification notification = (Notification)i.getParcelableExtra("NOTIFICATION"); NotificationManager notificationManager = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(requestCode, notification); } }
相关文章推荐
- 利用IE收听“广播”
- Android广播接收机制详细介绍(附短信接收实现)
- Jquery实现仿腾讯微博发表广播
- Android基于广播事件机制实现简单定时提醒功能代码
- Android实现静态广播监听器的方法
- Android中的广播、服务、数据库、通知、包等术语的原理和介绍(图解)
- Android中的广播(BroadCast)详细介绍
- Android中的广播和广播接收器代码实例
- Android开发之广播机制浅析
- Android 广播大全 Intent Action 事件详解
- 广播
- 网络广播风暴的几种原因
- 【原理总结】DHCP数据包分析
- 广播 BroadCastReceiver
- Envivio发布4Caster™ 广播3500实况H.264编码器
- BroadcastReceiver应用详解
- Android广播机制分析
- Android受限广播保护机制
- 跨平台UDP广播编程
- android 每次重启手机自己的程序会冒出已停止对话框