重启应用程序卸载

时间:2013-05-23 22:22:10

标签: android restart

使用setComponentEnabledSetting会导致android os在卸载时重新启动。还有其他人遇到过这个吗?

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

引起: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"/>

谢谢!

2 个答案:

答案 0 :(得分:0)

听起来您在活动中为android.intent.action.PACKAGE_CHANGED Intent注册了BroadcastReceiver,更改了状态,然后卸载了包。崩溃听起来像一个错误,但我猜测接收器仍然注册时通知管理器试图提供卸载然后崩溃。

您可能希望在卸载前调用setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

答案 1 :(得分:0)

我遇到了同样的问题,并通过不设置DONT_KILL_APP标志解决了这个问题。

getPackageManager().setComponentEnabledSetting(new ComponentName(this,
    FooBroadcastReceiver.class), receiverState, 0);