2

使用setComponentEnabledSetting会导致 android 操作系统在卸载时重新启动。有没有其他人遇到过这个?

错误/AndroidRuntime(1143):*系统进程中的致命异常:android.server.ServerThread java.lang.RuntimeException:接收广播 Intent 时出错 { act=android.intent.action.PACKAGE_CHANGED dat=package:com.example.foo flg= com.android.server.NotificationManagerService$2@41b00228 中的 0x8000010(有附加功能)}

引起:java.lang.IllegalArgumentException:未知包:com.example.foo 在 com.android.server.pm.Settings.getApplicationEnabledSettingLPr(Settings.java:2443)

这是代码:

public class FooActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();
        enableReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
        disableReceiver();
    }

    private void enableReceiver() {
        setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    }

    private void disableReceiver() {
        setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    }

    private void setReceiverState(int receiverState) {
        getPackageManager().setComponentEnabledSetting(new ComponentName(this, FooBroadcastReceiver.class), receiverState, PackageManager.DONT_KILL_APP);
    }

    public static class FooBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
        }
    }

}

清单中的接收者:

<receiver android:name="com.example.foo.FooActivity$FooBroadcastReceiver"/>

谢谢!

4

2 回答 2

0

It sounds like you registered a BroadcastReceiver for the android.intent.action.PACKAGE_CHANGED Intent inside your activity, changed its state and then uninstalled the package. The crash does sound like a bug, but I'm guessing while the receiver was still registered the notification manager tried to deliver the uninstall and then crashed.

You might want to call setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); before you uninstall?

于 2013-05-23T22:51:44.347 回答
0

我有同样的问题,并通过不设置DONT_KILL_APP标志来解决它。

getPackageManager().setComponentEnabledSetting(new ComponentName(this,
    FooBroadcastReceiver.class), receiverState, 0);
于 2013-09-18T12:55:17.340 回答