在新版本中创建一个Receiver接收手机应用的安装和卸载(可以监听到旧版本的卸载)
package com.justsy.lpi.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class PkInstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED" )) { String packageName = intent.getDataString().substring(8); System. out.println( "安装:" +packageName + "包名的程序" ); } //接收卸载广播 if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED" )) { String packageName = intent.getDataString().substring(8); System. out.println( "卸载:" + packageName + "包名的程序" ); Intent newIntent = new Intent(); newIntent.setClassName(packageName,packageName+ ".AutoStartProTestActivity" ); newIntent.setAction( "android.intent.action.MAIN"); newIntent.addCategory( "android.intent.category.LAUNCHER" ); newIntent.setFlags(Intent. FLAG_ACTIVITY_NEW_TASK); context.startActivity(newIntent); } } }
AndroidManifest.xml进行配置
<receiver android:name="com.justsy.lpi.receiver.PkInstallReceiver"
android:label=" @string/app_name">
<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>
<uses-permission android:name= "android.permission.RESTART_PACKAGES" />
<uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED" />