0

我有一个广播接收器(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 似乎根本无法接收广播意图。

4

1 回答 1

0

使用它在您的 onResume 中注册广播

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

请让我知道它是否解决了您的问题

于 2012-11-05T08:53:29.033 回答