开发者

Why does stopping my service kill the foreground activity?

开发者 https://www.devze.com 2023-02-19 09:39 出处:网络
In my app\'s main activity, I start a service like this: Intent i = new Intent(this, MyService.class);

In my app's main activity, I start a service like this:

Intent i = new Intent(this, MyService.class);
startService(i);

When the user presses a button, I stop the service like this:

Intent i = new Intent(this, MyService.class);
stopService(i);

When I do this, the foreground activity that's making the call is killed and disappears.

Thinking that maybe the service needed to run in a separate process, I tried setting android:process=":remote" on the service in the manifest, but that didn't change the behavoir.

It seems that I can't stop the service without killing the entire app. I开发者_JAVA技巧s there a way around this?


I figured out why the app was being killed. In my service's onDestroy() method, I was disabling a broadcast receiver like this:

PackageManager pacman = getPackageManager();
pacman.setComponentEnabledSetting(
     new ComponentName(this, MyReceiver.class), 
     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
     0);

This code disables a broadcast receiver that is defined in AndroidManifest.xml. The last argument, 0, should have been set to DONT_KILL_APP. I changed the code to this, and it solved the problem:

PackageManager pacman = getPackageManager();
pacman.setComponentEnabledSetting(
     new ComponentName(this, MyReceiver.class), 
     PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
     PackageManager.DONT_KILL_APP);


Without logs we can't know for sure but I have a good hunch that your app is throwing an exception and that you are silently catching and closing or you have an odd critical path that call finish.

In any case, what you described is not expected but we need more info to give a better answer

0

精彩评论

暂无评论...
验证码 换一张
取 消