Android 之 BroadcastReceiver (二) 常见的几个系统广播

来源:互联网 发布:注册域名后怎么建站 编辑:程序博客网 时间:2024/06/08 03:49

转载请注明出处:www.leoyanblog.com

本文出自 LeoYan 的博客

本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 LeoYan 即可关注。

常见的几个系统广播

系统启动完成

  我们经常会有这样的应用场合,比如消息推送服务,需要实现开机启动的功能。要实现这个功能,我们就可以订阅系统“启动完成”这条广播,接收到这条广播后我们就可以启动自己的服务了。我们来看一下BootCompleteReceiver和MsgPushService的具体实现:

public class BootCompleteReceiver extends BroadcastReceiver {      private static final String TAG = "BootCompleteReceiver";      @Override      public void onReceive(Context context, Intent intent) {          Intent service = new Intent(context, MsgPushService.class);          context.startService(service);          Log.i(TAG, "Boot Complete. Starting MsgPushService...");      }  }  
public class MsgPushService extends Service {      private static final String TAG = "MsgPushService";      @Override      public void onCreate() {          super.onCreate();          Log.i(TAG, "onCreate called.");      }      @Override      public int onStartCommand(Intent intent, int flags, int startId) {          Log.i(TAG, "onStartCommand called.");          return super.onStartCommand(intent, flags, startId);      }      @Override      public IBinder onBind(Intent arg0) {          return null;      }  }

  然后我们需要在 AndroidManifest.xml 中配置相关信息:

<!-- 开机广播接受者 -->  <receiver android:name=".BootCompleteReceiver">      <intent-filter>          <!-- 注册开机广播地址-->          <action android:name="android.intent.action.BOOT_COMPLETED"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>  <!-- 消息推送服务 -->  <service android:name=".MsgPushService"/> 

  我们看到 BootCompleteReceiver 注册了 “android.intent.action.BOOT_COMPLETED” 这个开机广播地址,从安全角度考虑,系统要求必须声明接收开机启动广播的权限,于是我们再声明使用下面的权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  

网络状态

  在某些场合,比如用户浏览网络信息时,网络突然断开,我们要及时地提醒用户网络已断开。要实现这个功能,我们可以接收网络状态改变这样一条广播,当由连接状态变为断开状态时,系统就会发送一条广播,我们接收到之后,再通过网络的状态做出相应的操作。下面就来实现一下这个功能:

public class NetworkStateReceiver extends BroadcastReceiver {    private static final String TAG = "NetworkStateReceiver";       @Override      public void onReceive(Context context, Intent intent) {          Log.i(TAG, "network state changed.");          if (!isNetworkAvailable(context)) {              Toast.makeText(context, "network disconnected!", 0).show();          }      }      /**      * 网络是否可用      *       * @param context      * @return      */      public static boolean isNetworkAvailable(Context context) {          ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);          NetworkInfo[] info = mgr.getAllNetworkInfo();          if (info != null) {              for (int i = 0; i < info.length; i++) {                  if (info[i].getState() == NetworkInfo.State.CONNECTED) {                      return true;                  }              }          }          return false;      }  }

  然后我们需要在 AndroidManifest.xml 中再注册一下这个广播接收者的信息:

<receiver android:name=".NetworkStateReceiver">      <intent-filter>          <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>   

  因为在 isNetworkAvailable 方法中我们使用到了网络状态相关的 API,所以需要声明相关的权限才行,下面就是对应的权限声明:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

电量变化

  如果我们阅读软件,可能是全屏阅读,这个时候用户就看不到剩余的电量,我们就可以为他们提供电量的信息。要想做到这一点,我们需要接收一条电量变化的广播,然后获取百分比信息,这听上去挺简单的,我们就来实现以下:

public class BatteryChangedReceiver extends BroadcastReceiver {      private static final String TAG = "BatteryChangedReceiver";      @Override      public void onReceive(Context context, Intent intent) {          // 当前电量          int currLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);         // 总电量          int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);         int percent = currLevel * 100 / total;          Log.i(TAG, "battery: " + percent + "%");      }  }

  然后我们需要在 AndroidManifest.xml 中再注册一下这个广播接收者的信息:

<receiver android:name=".BatteryChangedReceiver">      <intent-filter>          <action android:name="android.intent.action.BATTERY_CHANGED"/>          <category android:name="android.intent.category.DEFAULT" />      </intent-filter>  </receiver>  

  当然,有些时候我们是要立即获取电量的,而不是等电量变化的广播,比如当阅读软件打开时立即显示出电池电量。我们可以按以下方式获取:

Intent batteryIntent = getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));  int currLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);  int total = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);  int percent = currLevel * 100 / total;  Log.i("battery", "battery: " + percent + "%");  

监听SD卡状态

  清单文件中定义广播接收者接收的类型,监听SD卡常见的三种状态,所以广播接收者需要接收三种广播

<receiver android:name="com.leoyanblog.sdcradlistener.SDCardReceiver">    <intent-filter >        <action android:name="android.intent.action.MEDIA_MOUNTED"/>        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>        <action android:name="android.intent.action.MEDIA_REMOVED"/>        <data android:scheme="file"/>    </intent-filter></receiver>

  广播接收者的定义

public class SDCardReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // 区分接收到的是哪个广播        String action = intent.getAction();        if(action.equals("android.intent.action.MEDIA_MOUNTED")){            System.out.println("sd卡就绪");        }        else if(action.equals("android.intent.action.MEDIA_UNMOUNTED")){            System.out.println("sd卡被移除");        }        else if(action.equals("android.intent.action.MEDIA_REMOVED")){            System.out.println("sd卡被拔出");        }    }}

监听应用的安装、卸载、更新

  应用在安装卸载更新时,系统会发送广播,广播里会携带应用的包名,清单文件定义广播接收者接收的类型,因为要监听应用的三个动作,所以需要接收三种广播

<receiver android:name="com.leoyanblog.app.AppReceiver">    <intent-filter >        <action android:name="android.intent.action.PACKAGE_ADDED"/>        <action android:name="android.intent.action.PACKAGE_REPLACED"/>        <action android:name="android.intent.action.PACKAGE_REMOVED"/>        <data android:scheme="package"/>    </intent-filter></receiver>

  广播接收者的定义

public class AppReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //区分接收到的是哪种广播        String action = intent.getAction();        //获取广播中包含的应用包名        Uri uri = intent.getData();        if(action.equals("android.intent.action.PACKAGE_ADDED")){            System.out.println(uri + "被安装了");        }        else if(action.equals("android.intent.action.PACKAGE_REPLACED")){            System.out.println(uri + "被更新了");        }        else if(action.equals("android.intent.action.PACKAGE_REMOVED")){            System.out.println(uri + "被卸载了");        }    }}

常见的 Action 常量

  • ACTION_TIME_CHANGED:系统时间被改变。

  • ACTION_DATE_CHANGED:系统日期被改变。

  • ACTION_TIMEZONE_CHANGED:系统时区被改变。

  • ACTION_BOOT_COMPLETED:系统启动完成。

  • ACTION_PACKAGE_ADDED:系统添加包。

  • ACTION_PACKAGE_CHANGED:系统的包改变。

  • ACTION_PACKAGE_REMOVED:系统的包被删除。

  • ACTION_PACKAGE_RESTARTED:系统的包被重启。

  • ACTION_PACKAGE_DATA_CLEARED:系统的包数据被清空。

  • ACTION_BATTERY_CHANGED:电池电量改变。

  • ACTION_BATTERY_LOW:电池电量低。

  • ACTION_POWER_CONNECTED:系统连接电源。

  • ACTION_POWER_DISCONNECTED:系统与电源断开。

  • ACTION_SHUTDOWN:系统被关闭。

  通过使用BroadcastReceiver来监听特殊的广播,就可以让应用随系统执行特定的操作。

关注我的微信公众号,会有优质技术文章推送。

微信扫一扫下方二维码即可关注:
公众号

1 0
原创粉丝点击