使用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"/>
谢谢!