监听应用状态
2017-06-08 本文已影响0人
yanghanbin_it
监听应用安装,升级,卸载的广播
public class AppStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Uri uri = intent.getData();
if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
Toast.makeText(context, "有应用被安装:" + uri.toString(), Toast.LENGTH_SHORT).show();
} else if ("android.intent.action.PACKAGE_REPLACED".equals(action)) {
Toast.makeText(context, "有应用被升级" + uri.toString(), Toast.LENGTH_SHORT).show();
} else if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
Toast.makeText(context, "有应用被卸载" + uri.toString(), Toast.LENGTH_SHORT).show();
}
}
}
<receiver
android:name=".AppStateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_REPLACED"></action>
<action android:name="android.intent.action.PACKAGE_REMOVED"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>