本文来自:安卓航班网

从题目上看大家应该就能明白了,那么我就不多说了,现在我们就来看看代码是怎么写的吧。

java代码:

public class GetBroadcast extends BroadcastReceiver {

private static GetBroadcast mReceiver = new GetBroadcast();
private static IntentFilter mIntentFilter;


public static void registerReceiver(Context context) {
mIntentFilter
= new IntentFilter();
mIntentFilter.addDataScheme(
"package");
mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
context.registerReceiver(mReceiver, mIntentFilter);
}

public static void unregisterReceiver(Context context) {
context.unregisterReceiver(mReceiver);
}

@Override
public void onReceive(Context context, Intent intent) {
String action
= intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
Toast.makeText(context,
"有应用被添加", Toast.LENGTH_LONG).show();
}
else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
Toast.makeText(context,
"有应用被删除", Toast.LENGTH_LONG).show();
}

/*
* else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
* Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }
*/

else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Toast.makeText(context,
"有应用被替换", Toast.LENGTH_LONG).show();
}

/*
* else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
* Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }
*/

/*
* else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
* Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }
*/
}

}

原文地址:http://www.apkway.com/forum.php?mod=viewthread&tid=2984&extra=page%3D1

posted on 2011-08-27 19:36  泉と緣  阅读(510)  评论(0编辑  收藏  举报