应用安装和卸载

应用安装和卸载

  • 定义广播接收者 用来接收事件(应用安装和卸载的事件)
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("android.intent.action.PACKAGE_ADDED")){
//            System.out.println("应用安装了");
            Log.e(TAG, "onReceive: 应用安装了" );
        }
        if (action.equals("android.intent.action.PACKAGE_REMOVED")){
//            System.out.println("应用卸载了");
            Log.e(TAG, "onReceive: 应用卸载了" );
        }
    }
}
  • 清单文件配置
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

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

猜你喜欢

转载自www.cnblogs.com/nangongyibin/p/10230379.html