我有一个广播接收器( BackgroundReceiver ),它监听包的删除,然后将包名称广播到一个活动( AnalyzeActivity )
的 BackgroundReceiver.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
侦听自定义广播意图的 AnalyzeActivity.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);
}
}
};
但我的活动似乎根本无法接收广播意图。
答案 0 :(得分:0)
使用此命令在onResume中注册broacast
IntentFilter filter = new IntentFilter(BackgroundReceiver.REMOVED_INTENT);
mIntentReceiver intentToReceiveFilter = new mIntentReceiver();
registerReceiver(intentToReceiveFilter,filter);
并使用它从包删除广播接收器发送广播
Intent intent = new Intent(BackgroundReceiver.REMOVED_INTENT);
intent.putExtra("Package", pkg);
context.sendBroadcast(intent);
如果它解决了您的问题,请告诉我