您的位置:首页 > 移动开发 > Android开发

Android监听程序的安装和卸载

2015-07-01 17:11 393 查看
在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。应用程序无法监听自己的安装与卸载,但覆盖安装可以监听到自己的android.intent.action.PACKAGE_REMOVED广播。public class PackageReceiver extends BroadcastReceiver{@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) { String packageName = intent.getDataString(); Log.i("Test","---------------" + packageName); } if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { String packageName = intent.getDataString(); Log.i("Test","---------------" + "PACKAGE_REMOVED" + packageName); }}}复制代码<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="测试"> <receiver android:name=".PackageReceiver" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RESTART_PACKAGES"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/></manifest>这里是手动注册广播
public
class
GetBroadcast
extends
BroadcastReceiver
 {
02
03
private
static
GetBroadcast
 mReceiver =
new
GetBroadcast();
04
private
static
IntentFilter
 mIntentFilter;
05
06
07
public
static
void
registerReceiver(Context
 context) {
08
mIntentFilter
 =
new
IntentFilter();
09
mIntentFilter.addDataScheme(
"package"
);
10
mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
11
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
12
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
13
context.registerReceiver(mReceiver,
 mIntentFilter);
14
}
15
16
public
static
void
unregisterReceiver(Context
 context) {
17
context.unregisterReceiver(mReceiver);
18
}
19
20
@Override
21
public
void
onReceive(Context
 context, Intent intent) {
22
String
 action = intent.getAction();
23
if
(Intent.ACTION_PACKAGE_ADDED.equals(action))
 {
24
Toast.makeText(context,
"有应用被添加"
,
 Toast.LENGTH_LONG).show();
25
}
else
if
(Intent.ACTION_PACKAGE_REMOVED.equals(action))
 {
26
Toast.makeText(context,
"有应用被删除"
,
 Toast.LENGTH_LONG).show();
27
}
28
29
/*
30
*
 else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
31
*
Toast.makeText(context,"有应用被改变", Toast.LENGTH_LONG).show(); }
32
*/
33
34
else
 if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
35
Toast.makeText(context,
 "有应用被替换", Toast.LENGTH_LONG).show();
36
}
37
38
/*
39
*
 else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
40
*
Toast.makeText(context,"有应用被重启", Toast.LENGTH_LONG).show(); }
41
*/
42
43
/*
44
*
 else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
45
*
Toast.makeText(context,"有应用被安装", Toast.LENGTH_LONG).show(); }
46
*/
47
}
48
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: