This article describes the Android programming method of monitoring APK installation and deletion. Share it for your reference, as follows:
A series of action monitoring after software download: the status was obtained through Service monitoring scan, and then this method was used to test it.
import ; import ; import ; import ; public class getBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_ADDED.equals(())){ (context, "Applications have been added", Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REMOVED.equals(())){ (context, "Applications have been deleted", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_CHANGED.equals(())){ (context, "The application has been changed", Toast.LENGTH_LONG).show(); }*/ else if(Intent.ACTION_PACKAGE_REPLACED.equals(())){ (context, "Applications are replaced", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(())){ (context, "Application has been restarted", Toast.LENGTH_LONG).show(); }*/ /* else if(Intent.ACTION_PACKAGE_INSTALL.equals(())){ (context, "The application is installed", Toast.LENGTH_LONG).show(); }*/ } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Broadcast" android:label="@string/app_name"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <receiver android:name="getBroadcast" android:enabled="true" > <intent-filter> <action android:name=".PACKAGE_ADDED"></action> <!-- <action android:name=".PACKAGE_CHANGED"></action>--> <action android:name=".PACKAGE_REMOVED"></action> <action android:name=".PACKAGE_REPLACED"></action> <!-- <action android:name=".PACKAGE_RESTARTED"></action>--> <!-- <action android:name=".PACKAGE_INSTALL"></action>--> <data android:scheme="package"></data> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
Code implementation added:
private final BroadcastReceiver apkInstallListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_ADDED.equals(())){ ("**************Broadcase*************"); File file = (isDeleted); (()+"*****"); (); //(()+"(*******"+()); if(uninstallApk!=null&&uApks!=null) { (isDeleted); (isDeleted); } //Clear the value in the collection if(uninstallApk!=null) { ("onpause******"+()); (); } if(uApks!=null) { (); } ("***Apply Add***"+isDeleted); (context, "Applications have been added"+isDeleted, Toast.LENGTH_LONG).show(); } else if(Intent.ACTION_PACKAGE_REMOVED.equals(())){ ("***app deleted"); (context, "Applications have been deleted", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_CHANGED.equals(())){ (context, "The application has been changed", Toast.LENGTH_LONG).show(); }*/ else if(Intent.ACTION_PACKAGE_REPLACED.equals(())){ ("****app was replaced"); (context, "Applications are replaced", Toast.LENGTH_LONG).show(); } /* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(())){ (context, "Application has been restarted", Toast.LENGTH_LONG).show(); }*/ /* else if(Intent.ACTION_PACKAGE_INSTALL.equals(())){ (context, "The application is installed", Toast.LENGTH_LONG).show(); }*/ } }; // Register to monitorprivate void registerSDCardListener(){ IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); (Intent.ACTION_PACKAGE_ADDED); (Intent.ACTION_PACKAGE_REMOVED); (Intent.ACTION_PACKAGE_REPLACED); ("package"); registerReceiver(apkInstallListener, intentFilter); }
Calls in javaregisterSDCardListener()
@Override protected void onDestroy() { (); //unregisterReceiver(apkInstallListener); }
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.