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(); }

*/

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐