您的位置:首页 > 移动开发 > Android开发

Android广播机制

2015-10-29 15:03 525 查看
动态注册系统广播

系统广播不用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); //取消监听网络的广播注册
}


//静态注册系统广播
<​m​a​n​i​f​e​s​t​ ​x​m​l​n​s​:​a​n​d​r​o​i​d​=​"​h​t​t​p​:​/​/​s​c​h​e​m​a​s​.​a​n​d​r​o​i​d​.​c​o​m​/​a​p​k​/​r​e​s​/​a​n​d​r​o​i​d​"​
​ ​p​a​c​k​a​g​e​=​"​c​o​m​.​b​i​g​n​e​r​d​r​a​n​c​h​.​a​n​d​r​o​i​d​.​p​h​o​t​o​g​a​l​l​e​r​y​"​
​ ​a​n​d​r​o​i​d​:​v​e​r​s​i​o​n​C​o​d​e​=​"​1​"​
​ ​a​n​d​r​o​i​d​:​v​e​r​s​i​o​n​N​a​m​e​=​"​1​.​0​"​ ​>​

​ ​<​u​s​e​s​-​s​d​k​
​ ​ ​ ​a​n​d​r​o​i​d​:​m​i​n​S​d​k​V​e​r​s​i​o​n​=​"​8​"​
​ ​ ​ ​a​n​d​r​o​i​d​:​t​a​r​g​e​t​S​d​k​V​e​r​s​i​o​n​=​"​1​7​"​ ​/​>​

​ ​<​u​s​e​s​-​p​e​r​m​i​s​s​i​o​n​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​a​n​d​r​o​i​d​.​p​e​r​m​i​s​s​i​o​n​.​I​N​T​E​R​N​E​T​"​ ​/​>​
​ ​<​u​s​e​s​-​p​e​r​m​i​s​s​i​o​n​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​a​n​d​r​o​i​d​.​p​e​r​m​i​s​s​i​o​n​.​A​C​C​E​S​S​_​N​E​T​W​O​R​K​_​S​T​A​T​E​"​ ​/​>​
​ ​<​u​s​e​s​-​p​e​r​m​i​s​s​i​o​n​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​a​n​d​r​o​i​d​.​p​e​r​m​i​s​s​i​o​n​.​R​E​C​E​I​V​E​_​B​O​O​T​_​C​O​M​P​L​E​T​E​D​"​ ​/​>​

​ ​<​a​p​p​l​i​c​a​t​i​o​n​
​ ​ ​ ​.​.​.​ ​>​
​ ​ ​ ​<​a​c​t​i​v​i​t​y​
​ ​ ​ ​ ​ ​.​.​.​ ​>​
​ ​ ​ ​ ​ ​.​.​.​
​ ​ ​ ​<​/​a​c​t​i​v​i​t​y​>​
​ ​ ​ ​<​s​e​r​v​i​c​e​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​.​P​o​l​l​S​e​r​v​i​c​e​"​ ​/​>​
​ ​ ​ ​<​r​e​c​e​i​v​e​r​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​.​S​t​a​r​t​u​p​R​e​c​e​i​v​e​r​"​>​
​ ​ ​ ​ ​ ​ ​ ​<​i​n​t​e​n​t​-​f​i​l​t​e​r​>​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​<​a​c​t​i​o​n​ ​a​n​d​r​o​i​d​:​n​a​m​e​=​"​a​n​d​r​o​i​d​.​i​n​t​e​n​t​.​a​c​t​i​o​n​.​B​O​O​T​_​C​O​M​P​L​E​T​E​D​"​ ​/​>​
​ ​ ​ ​ ​ ​ ​ ​<​/​i​n​t​e​n​t​-​f​i​l​t​e​r​>​
​ ​ ​ ​<​/​r​e​c​e​i​v​e​r​>​
​ ​<​/​a​p​p​l​i​c​a​t​i​o​n​>​

<​/​m​a​n​i​f​e​s​t​>​
public class StartupReceiver{

@​O​v​e​r​r​i​d​e​
p​u​b​l​i​c​ ​v​o​i​d​ ​o​n​R​e​c​e​i​v​e​(​C​o​n​t​e​x​t​ ​c​o​n​t​e​x​t​,​ ​I​n​t​e​n​t​ ​i​n​t​e​n​t​)​ ​{​
​ ​ ​ ​L​o​g​.​i​(​T​A​G​,​ ​"​R​e​c​e​i​v​e​d​ ​b​r​o​a​d​c​a​s​t​ ​i​n​t​e​n​t​:​ ​"​ ​+​ ​i​n​t​e​n​t​.​g​e​t​A​c​t​i​o​n​(​)​)​;​

​ ​ ​ //静态注册的广播 文中广播是监听程序启动完成状态,监听到就会走onReceive()方法 可以在这里加代码开启操作
}​
}


发送普通广播

首先定义一个广播 intentfilter 名字 p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​S​t​r​i​n​g​ ​A​C​T​I​O​N​_​S​H​O​W​_​N​O​T​I​F​I​C​A​T​I​O​N​ ​=​
​ ​ ​ ​ ​ ​ ​ ​"​c​o​m​.​b​i​g​n​e​r​d​r​a​n​c​h​.​a​n​d​r​o​i​d​.​p​h​o​t​o​g​a​l​l​e​r​y​.​S​H​O​W​_​N​O​T​I​F​I​C​A​T​I​O​N​"​;​

发送广播s​e​n​d​B​r​o​a​d​c​a​s​t​(​n​e​w​ ​I​n​t​e​n​t​(​A​C​T​I​O​N​_​S​H​O​W​_​N​O​T​I​F​I​C​A​T​I​O​N​)​)​;​

//广播接收端 动态自定义注册
p​u​b​l​i​c​ ​a​b​s​t​r​a​c​t​ ​c​l​a​s​s​ ​V​i​s​i​b​l​e​F​r​a​g​m​e​n​t​ ​e​x​t​e​n​d​s​ ​F​r​a​g​m​e​n​t​ ​{​
​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​S​t​r​i​n​g​ ​T​A​G​ ​=​ ​"​V​i​s​i​b​l​e​F​r​a​g​m​e​n​t​"​;​

​ ​ ​ ​p​r​i​v​a​t​e​ ​B​r​o​a​d​c​a​s​t​R​e​c​e​i​v​e​r​ ​m​O​n​S​h​o​w​N​o​t​i​f​i​c​a​t​i​o​n​ ​=​ ​n​e​w​ ​B​r​o​a​d​c​a​s​t​R​e​c​e​i​v​e​r​(​)​ ​{​
​ ​ ​ ​ ​ ​ ​ ​@​O​v​e​r​r​i​d​e​
​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​o​n​R​e​c​e​i​v​e​(​C​o​n​t​e​x​t​ ​c​o​n​t​e​x​t​,​ ​I​n​t​e​n​t​ ​i​n​t​e​n​t​)​ ​{​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​T​o​a​s​t​.​m​a​k​e​T​e​x​t​(​g​e​t​A​c​t​i​v​i​t​y​(​)​,​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​"​G​o​t​ ​a​ ​b​r​o​a​d​c​a​s​t​:​"​ ​+​ ​i​n​t​e​n​t​.​g​e​t​A​c​t​i​o​n​(​)​,​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​T​o​a​s​t​.​L​E​N​G​T​H​_​L​O​N​G​)​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​.​s​h​o​w​(​)​;​
​ ​ ​ ​ ​ ​ ​ ​}​
​ ​ ​ ​}​;​

​ ​ ​ ​@​O​v​e​r​r​i​d​e​
​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​o​n​R​e​s​u​m​e​(​)​ ​{​
​ ​ ​ ​ ​ ​ ​ ​s​u​p​e​r​.​o​n​R​e​s​u​m​e​(​)​;​
​ ​ ​ ​ ​ ​ ​ ​I​n​t​e​n​t​F​i​l​t​e​r​ ​f​i​l​t​e​r​ ​=​ ​n​e​w​ ​I​n​t​e​n​t​F​i​l​t​e​r​(​P​o​l​l​S​e​r​v​i​c​e​.​A​C​T​I​O​N​_​S​H​O​W​_​N​O​T​I​F​I​C​A​T​I​O​N​)​;​
​ ​ ​ ​ ​ ​ ​ ​g​e​t​A​c​t​i​v​i​t​y​(​)​.​r​e​g​i​s​t​e​r​R​e​c​e​i​v​e​r​(​m​O​n​S​h​o​w​N​o​t​i​f​i​c​a​t​i​o​n​,​ ​f​i​l​t​e​r​)​;​
​ ​ ​ ​}​

​ ​ ​ ​@​O​v​e​r​r​i​d​e​
​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​o​n​P​a​u​s​e​(​)​ ​{​
​ ​ ​ ​ ​ ​ ​ ​s​u​p​e​r​.​o​n​P​a​u​s​e​(​)​;​
​ ​ ​ ​ ​ ​ ​ ​g​e​t​A​c​t​i​v​i​t​y​(​)​.​u​n​r​e​g​i​s​t​e​r​R​e​c​e​i​v​e​r​(​m​O​n​S​h​o​w​N​o​t​i​f​i​c​a​t​i​o​n​)​;​
​ ​ ​ ​}​
}​

动态发送有序化广播

首先定义一个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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  广播