安卓开发之应用的卸载与安装(广播接收者案例)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lidaochen.test001"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".AppStateReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_INSTALL"></action> <action android:name="android.intent.action.PACKAGE_ADDED"></action> <action android:name="android.intent.action.PACKAGE_REMOVED"></action> <!-- 想让action事件生效 还需要配置一个data --> <data android:scheme="package"></data> </intent-filter> </receiver> </application></manifest>
package com.lidaochen.test001;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class AppStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 获取当前广播事件的类型 String action = intent.getAction(); if("android.intent.action.PACKAGE_INSTALL".equals(action)) { System.out.println("应用安装了111"); } // 最后执行的是这个 else if("android.intent.action.PACKAGE_ADDED".equals(action)) { System.out.println("应用安装了222"); } else if("android.intent.action.PACKAGE_REMOVED".equals(action)) { System.out.println("应用卸载了222" + intent.getData()); } }}

 

相关文章