android軟件開發:後臺監控應用程序包的安裝&卸載

方法一:
public class getBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){
Toast.makeText(context, "按鍵", Toast.LENGTH_LONG).show();
}
}
}
public class getBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
Toast.makeText(context, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){
Toast.makeText(context, "按鍵", Toast.LENGTH_LONG).show();
}
}
}
需要聲明的權限如下AndroidManifest.xml
<?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">
<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>
<receiver 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>-->
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</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">
<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>
<receiver 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>-->
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
方法二:
通過閱讀Android SDK 裏關於intent.action 這部分裏面的描述,我們可以找到一些與package
相關的系統廣播
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
其中ACTION_PACKAGE_ADDED 在SDK 裏的描述是:
Broadcast Action: A new application package has been installed on the device.
ACTION_PACKAGE_REMOVED 在SDK 裏的描述是:
Broadcast Action: An existing application package has been removed from the device.
ACTION_PACKAGE_REPLACED 在SDK 裏的描述是:
Broadcast Action: A new version of an application package has been installed, replacing an
existing version that was previously installed.
通過這三個廣播消息我們已經可以監控到Android 應用程序的安裝和刪除
詳細的實現代碼如下
getBroadcast.java
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, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啓", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", 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, "有應用被添加", Toast.LENGTH_LONG).show();
}
else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
Toast.makeText(context, "有應用被刪除", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
Toast.makeText(context, "有應用被改變", Toast.LENGTH_LONG).show();
}*/
else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
Toast.makeText(context, "有應用被替換", Toast.LENGTH_LONG).show();
}
/* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
Toast.makeText(context, "有應用被重啓", Toast.LENGTH_LONG).show();
}*/
/* else if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
Toast.makeText(context, "有應用被安裝", Toast.LENGTH_LONG).show();
}*/
}
}
然後在AndroidManifest.xml 中聲明這幾個Action 的<intent-filter>即可在系統裏捕獲這些廣
播消息
具體的源代碼如下
view plaincopy to clipboardprint?
<?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">
<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>
<receiver 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>
</receiver>
</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">
<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>
<receiver 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>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
把程序安裝之後,系統就會註冊這個BroadcastReceiver
然後有應用安裝刪除替換操作時時,就會彈出Toast 提示
發佈了81 篇原創文章 · 獲贊 65 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章