Whether the app in the broadcast listening mobile phone is installed and uninstalled, and listens to the app
Public class PackageReceiver extends BroadcastReceiver {
Private static final int PACKAGE_NAME_INDEX = 8;
@ Override
Public void onReceive (Context context, Intent intent ){
If (intent! = Null ){
/** The installer's 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 (isavilile (context, packageName )){
/** Indicates that the application package is installed */
Uri uri = Uri. fromParts ("package", packageName, null );
/** Listen to the unmounted ACTION of the application */
Intent = new Intent (Intent. ACTION_DELETE, uri );
Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (intent );
}
Else {
Toast. makeText (context, "this application is not installed on the mobile phone! ", Toast. LENGTH_SHORT). show ();
}
}
}
}
/** Check whether the client is installed */
Public static boolean isAvilible (Context context, String packageName ){
/** PackageManager */
Final PackageManager packageManager = context. getPackageManager ();
/** Package information of all installed programs */
List <PackageInfo> pinfo = packageManager. getInstalledPackages (0 );
/** Store the package names 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 );
}
}
----------- Register broadcast in AndroidManifest. xml ---------------------------
<Cycler android: name = ". PackageReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. PACKAGE_ADDED"/>
<Data android: scheme = "package"/>
</Intent-filter>
</Cycler>