This article mainly discusses how to monitor the installation and deletion of Android packages.
The Installation Method of the Android system has been described in detail in "detailed investigation of Android application installation methods". The link is as follows:
Http://www.bkjia.com/kf/201201/116677.html
Based on these installation methods, how can we monitor system installation?
By reading the description in the intent. action section of the Android SDK, we can find some System Broadcasts related to the package.
[C-sharp] android. intent. action. PACKAGE_ADDED
Android. intent. action. PACKAGE_CHANGED
Android. intent. action. PACKAGE_DATA_CLEARED
Android. intent. action. PACKAGE_INSTALL
Android. intent. action. PACKAGE_REMOVED
Android. intent. action. PACKAGE_REPLACED
Android. intent. action. PACKAGE_RESTARTED
Android. intent. action. PACKAGE_ADDED
Android. intent. action. PACKAGE_CHANGED
Android. intent. action. PACKAGE_DATA_CLEARED
Android. intent. action. PACKAGE_INSTALL
Android. intent. action. PACKAGE_REMOVED
Android. intent. action. PACKAGE_REPLACED
Android. intent. action. PACKAGE_RESTARTED
Where
ACTION_PACKAGE_ADDED
The description in the SDK is
Broadcast Action: A new application package has been installed on the device.
ACTION_PACKAGE_REMOVED
The description in the SDK is
Broadcast Action: An existing application package has been removed from the device.
ACTION_PACKAGE_REPLACED
The description in the SDK is
Broadcast Action: A new version of an application package has been installed, replacing an existing version that was previusly installed.
With these three broadcast messages, we can monitor the installation and deletion of Android applications.
The detailed implementation code is as follows:
GetBroadcast. java
[C-sharp] package zy. Broadcast;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. Toast;
Public class getBroadcast extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
If (Intent. ACTION_PACKAGE_ADDED.equals (intent. getAction ())){
Toast. makeText (context, "added applications", Toast. LENGTH_LONG). show ();
}
Else if (Intent. ACTION_PACKAGE_REMOVED.equals (intent. getAction ())){
Toast. makeText (context, "deleted Applications", Toast. LENGTH_LONG). show ();
}
/* Else if (Intent. ACTION_PACKAGE_CHANGED.equals (intent. getAction ())){
Toast. makeText (context, "Application changed", Toast. LENGTH_LONG). show ();
}*/
Else if (Intent. ACTION_PACKAGE_REPLACED.equals (intent. getAction ())){
Toast. makeText (context, "application replaced", Toast. LENGTH_LONG). show ();
}
/* Else if (Intent. ACTION_PACKAGE_RESTARTED.equals (intent. getAction ())){
Toast. makeText (context, "an application has been restarted", Toast. LENGTH_LONG). show ();
}*/
/* Else if (Intent. ACTION_PACKAGE_INSTALL.equals (intent. getAction ())){
Toast. makeText (context, "applications installed", Toast. LENGTH_LONG). show ();
}*/
}
}
Package zy. Broadcast;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. Toast;
Public class getBroadcast extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
If (Intent. ACTION_PACKAGE_ADDED.equals (intent. getAction ())){
Toast. makeText (context, "added applications", Toast. LENGTH_LONG). show ();
}
Else if (Intent. ACTION_PACKAGE_REMOVED.equals (intent. getAction ())){
Toast. makeText (context, "deleted Applications", Toast. LENGTH_LONG). show ();
}
/* Else if (Intent. ACTION_PACKAGE_CHANGED.equals (intent. getAction ())){
Toast. makeText (context, "Application changed", Toast. LENGTH_LONG). show ();
}*/
Else if (Intent. ACTION_PACKAGE_REPLACED.equals (intent. getAction ())){
Toast. makeText (context, "application replaced", Toast. LENGTH_LONG). show ();
}
/* Else if (Intent. ACTION_PACKAGE_RESTARTED.equals (intent. getAction ())){
Toast. makeText (context, "an application has been restarted", Toast. LENGTH_LONG). show ();
}*/
/* Else if (Intent. ACTION_PACKAGE_INSTALL.equals (intent. getAction ())){
Toast. makeText (context, "applications installed", Toast. LENGTH_LONG). show ();
}*/
}
}
Then, the <intent-filter> of these actions can be declared in AndroidManifest. xml to capture these broadcast messages in the system.
The specific source code is as follows:
[C-sharp] <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "zy. Broadcast"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". Broadcast"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Cycler android: name = "getBroadcast" android: enabled = "true">
<Intent-filter>
<Action android: name = "android. intent. action. PACKAGE_ADDED"> </action>
<! -- <Action android: name = "android. intent. action. PACKAGE_CHANGED"> </action> -->
<Action android: name = "android. intent. action. PACKAGE_REMOVED"> </action>
<Action android: name = "android. intent. action. PACKAGE_REPLACED"> </action>
<! -- <Action android: name = "android. intent. action. PACKAGE_RESTARTED"> </action> -->
<! -- <Action android: name = "android. intent. action. PACKAGE_INSTALL"> </action> -->
<Data android: scheme = "package"> </data>
</Intent-filter>
</Cycler>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
</Manifest>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "zy. Broadcast"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". Broadcast"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Cycler android: name = "getBroadcast" android: enabled = "true">
<Intent-filter>
<Action android: name = "android. intent. action. PACKAGE_ADDED"> </action>
<! -- <Action android: name = "android. intent. action. PACKAGE_CHANGED"> </action> -->
<Action android: name = "android. intent. action. PACKAGE_REMOVED"> </action>
<Action android: name = "android. intent. action. PACKAGE_REPLACED"> </action>
<! -- <Action android: name = "android. intent. action. PACKAGE_RESTARTED"> </action> -->
<! -- <Action android: name = "android. intent. action. PACKAGE_INSTALL"> </action> -->
<Data android: scheme = "package"> </data>
</Intent-filter>
</Cycler>
</Application>
<Uses-sdk android: minSdkVersion = "7"/>
</Manifest>
After the program is installed, the system registers the BroadcastReceiver
The Toast prompt is displayed when an application is installed, deleted, or replaced.
Delete An Application
Add Application
Application replaced
In this way, we can monitor the installation process of Android applications.
As for the interception of the installation process, I am also working on it. You can share it with me if you have a good idea. Thank you.
From Zy's technical experience