public class Packagereceiver extends Broadcastreceiver {
private static final int package_name_index = 8;
@Override
public void OnReceive (context context, Intent Intent) {
if (intent! = NULL) {
/** Installer ACTION is intent.action_package_added*/
if (Intent.getaction (). Equals (intent.action_package_added)) {
String data = intent.getdatastring ();
if (data = = NULL | | data.length () <= package_name_index) {return;}
String PackageName = data.substring (Package_name_index);
if (isavilible (context, PackageName)) {
/** Description Installed App Package */
Uri uri = uri.fromparts ("package", PackageName, NULL);
/** Monitoring Application Uninstall action*/
Intent = new Intent (Intent.action_delete, URI);
Intent.addflags (Intent.flag_activity_new_task);
Context.startactivity (Intent);
}
else {
Toast.maketext (Context, "The phone doesn't have the app installed! ", Toast.length_short). Show ();
}
}
}
}
/** detects if the client is installed */
public static Boolean isavilible (context context, String PackageName) {
/** Package Manager packagemanager*/
Final Packagemanager Packagemanager = Context.getpackagemanager ();
/** package information for all installed programs */
List<packageinfo> pinfo = packagemanager.getinstalledpackages (0);
/** Store the package name of all installed programs */
list<string> pName = new arraylist<string> ();
if (pinfo! = null) {
for (int i = 0; i < pinfo.size (); i++) {
String pn = Pinfo.get (i). PackageName;
Pname.add (PN);
}
}
Return Pname.contains (PackageName);
}
}
-----------Registration of broadcast---------------------------in Androidmanifest.xml
<receiver android:name= ". Packagereceiver ">
<intent-filter>
<action android:name= "Android.intent.action.PACKAGE_ADDED"/>
<data android:scheme= "Package"/>
</intent-filter>
</receiver>
Broadcast monitor whether the app is installed and uninstalled in the phone