我有一个广播接收器(BackgroundReceiver),它监听包删除,然后将包名称广播到一个活动(AnalyzeActivity)
背景接收器.java
public static final String REMOVED_INTENT = "com.analyze.project.RemovedReceiver";
public void onReceive(Context context, Intent intent) {
String actionStr = intent.getAction();
if (Intent.ACTION_PACKAGE_REMOVED.equals(actionStr)) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
Log.i("Logging Service", "Package Removed: " + pkg);
//Toast.makeText(context, "Package Removed:" + pkg, Toast.LENGTH_SHORT).show();
Intent c = new Intent();
c.setAction(REMOVED_INTENT);
c.putExtra("Package", pkg);
context.sendBroadcast(c);
}
}
我在我的AnalyzeActivity中动态注册接收器并监听来自BackgroundReceiver的自定义广播意图
分析活动.java
@Override
protected void onResume() {
super.onResume();
// Register Sync Recievers
IntentFilter intentToReceiveFilter = new IntentFilter();
intentToReceiveFilter.addAction(BackgroundReceiver.REMOVED_INTENT);
this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler);
mReceiversRegistered = true;
}
final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String Package = intent.getStringExtra("Package");
if (intent.getAction().equals(BackgroundReceiver.REMOVED_INTENT)) {
Toast.makeText(context, "(A) Package Removed: " + Package, Toast.LENGTH_SHORT).show();
Log.i("Package Removed", "(A) Package Removed: " + Package);
}
}
};
但我的 Activity 似乎根本无法接收广播意图。