监听Package的安装和卸载、电源的连接和断开、网络状态改变广播、解锁广播、屏幕变亮变按广播

            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
                <action android:name="android.intent.action.PACKAGE_DATA_CLEARED" />
                <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
                <action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
        

                <data android:scheme="package" />
            </intent-filter>

已经安装了一个应用,然后更新一个新的版本,发出的系统广播依次是   REMOVED, ADDED, REPLACED

卸载一个应用,发出的广播是 REMOVED

安装一个应用是 ADDED

在应用管理中清除一个应用的数据,发出的广播是 DATA_CLEARED

腾讯微博,游戏,ZAKER等应用都是接收了 应用的安装和卸载的广播来拉起后台服务的(还有网络变化的广播)

------------------------------------------------------------------------------------------------------------

 <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
 <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />

电源连接和断开的时候会发出这个广播

 -----------------------------------------------------------------------------------------------------------

监听网络状态改变

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

 

receiver android:name="you_package_name.ConnectionChangeReceiver" 

  < intent-filter>   

     < action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   

  < /intent-filter>   

/receiver> 

 

onReceive中:

NetworkInfo networkInfo = intent .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

String extraInfo = intent .getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
boolean isFailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo otherNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);

 -----------------------------------------------------------------------------------------------------------------

屏幕变亮和变暗

android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON

在AndroidManifest.xml中静态注册是不可以的。只能动态注册去监听这个事件。

这个问题我的理解是google故意这么做的,有两点考虑:
1.提高监听screen_on screen_off门槛
这两个事件是android的基本事件,如果呗大多数程序监听,会大大的拖慢整个系统,所以android也应该不鼓励我们在后台监听这两个事件。

registerReceiver(new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
  // ... 
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

---------------------------------------------------------------------------------------------------------------------

那怎么才能保证我的服务一直是启动状态呢,其实还有另一个Action可以反映出用户正在使用手机的行为,每个用户在使用手机的时候,首先按电源键将屏幕点亮,紧接着就是解锁。解锁动作通过android.intent.action.USER_PRESENT发送出来,我们就能识别出该用户进入了home界面,也就能启动我们相应的服务了,不管你是要谈对话框welcome用户,还是后台启动程序升级服务,都可以!以神的名义发誓,该Action在AndroidManifest.xml中可以监听得到。

<action android:name="android.intent.action.USER_PRESENT" />

Since: API Level 3 Broadcast Action: Sent when the user is present after device wakes up (e.g when the keyguard is gone). This is a protected intent that can only be sent by the system. 每个用户在使用手机的时候,首先按电源键将屏幕点亮,紧接着就是解锁。解锁动作通过android.intent.action.USER_PRESENT发送出来,我们就能识别出该用户进入了home界面

屏幕解锁的时候会触发这个广播。会受好多条件的制约。 比如有些第三方桌面不会真正的锁屏,360,LBE等安全软件对广播的拦截等等

Intent.ACTION_SCREEN_OFF = "android.intent.action.SCREEN_OFF" 通过监听屏幕SCREEN_ON和SCREEN_OFF这两个action。奇怪的是,这两个action只能通过代码动态的形式注册,才能被监听到,使用AndroidManifest.xml 完全监听不到

原文地址:https://www.cnblogs.com/zijianlu/p/2869910.html