标签:
有序广播
动态创建广播:
创建一个类继承BroadcastReceiver,重写onReceive,有广播来到就会执行onReceive方法,动态注册的一定要取消注册.
public class MainActivity extends Activity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 当系统发生变化时发出的是值为括号内的广播,想要监听什么广播就添加相应的action
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);// 注册广播
}
protected void onDestroy() {
super.onDestroy();
// 动态注册的一定要取消注册
unregisterReceiver(networkChangeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver {
// 每当网络发生变化就会执行此方法
public void onReceive(Context arg0, Intent arg1) {
//系统服务类,用来管理网络连接
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
Toast.makeText(MainActivity.this, "网络可用", 0).show();
}else{
Toast.makeText(MainActivity.this, "网络不可用", 0).show();
}
}
}
}
静态注册实现开机自启
创建一个类,继承自BroadcastReceiver,收到广播就会执行onReceive方法
public class BootCompleteReceiver extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "开机",0).show();
}}
清单文件注册:
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
发送自定义广播:
首先构建一个intent对象,然后调用Context的sendBroadcast()方法把广播发送出去,这样所有监听这条广播的都会接受到消息
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
发送有序广播
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent, null);
本地广播
在应用程序的内部进行传递,并且广播接收器只能接受来自本应用的广播
public class MainActivity extends Activity {
private LocalReceiver localReceiver;
private IntentFilter intentFilter;
private LocalBroadcastManager localBroadcastManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);
//注册本地广播监听器
}
public void click(View v){
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);//发送本地广播
}
class LocalReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "received local broadcast",
Toast.LENGTH_SHORT).show();
}
}
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("强制下线");
builder.setMessage("在他处登录,你被强制下线");
builder.setCancelable(false);//设置不可取消
builder.setPositiveButton("OK",new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//TODO
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
标签:
原文地址:http://www.cnblogs.com/muhao/p/5157773.html