BroadCast
2017-07-13 本文已影响0人
熊大哥87
网络状态监听
intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver=new NetworkChangeRecevier();
registerReceiver(networkChangeReceiver,intentFilter);
class NetworkChangeRecevier extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if (networkInfo!=null&&networkInfo.isAvailable()){
Toast.makeText(context,"network is available",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"network is unavailable",Toast.LENGTH_SHORT).show();
}
}
}
权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
开机广播监听
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot complete",Toast.LENGTH_LONG).show();
}
}
权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
发送广播
发送标准广播
定义发送广播
intent-filter>
<action android:name="text.broadcast.MY_BROAD"/></intent-filter>
意图发送
ntent intent=new Intent("text.broadcast.MY_BROAD");
sendBroadcast(intent);
定义一个类接受广播继承BroadCastReceiver