Android service does not restart after it has been killed many times in a row

I created a simple Android application to solve an annoying problem. When you join the wireless network at my university, you are redirected to the login page where you must provide your student number and password.

I have an Android app that allows you to enter your data, then the service registers the receiver with the WifiManager.NETWORK_STATE_CHANGED_ACTION intent WifiManager.NETWORK_STATE_CHANGED_ACTION . Whenever a device connects to a university network, a webrequest HTTP message is generated with a detailed log. Thus, solving the redirection problem.

Everything works fine ... However, in my advanced testing, I found a very strange problem. When I press the “Power Master” button once and then open the “Settings”> “Applications”> “Launch”, I see that my service is restarting (along with many other services, for example Facebook). After about 30 seconds, the service will reboot and everything will be in order.

However, if I press the lightning button repeatedly (3 or more) in quick succession, and then go to "Settings"> "Applications"> "Launch", I no longer see my service. He never seemed to try to restart him. Facebook and other services go through the normal restart phase and ultimately succeed.

Does anyone know what is going on? Why does my service die forever when I press the lightning button several times?

Before you find out that I changed my application to work with startForeground() , and this works great in all my advanced testing. This, however, is not a valid solution because I do not want my application to receive constant notification in the status bar.

PS My project works on github if you want to recreate this strange problem https://github.com/ccoffey/NUIMWiFi

PPS "Go Power Master" application is a free application on Google Play, should the lightning button return lost RAM? I think this makes all background services die. This is definitely what it seems to be all the same.

Edited to include LogCat logs

Well, I partially answered my question. Below is a LogCat message for my service, filtered only to enable ie.cathalcoffey.android. I highlighted the lines that matter. It turns out that every time you kill a service, its restart time increases. This was originally 135096 ms, the second time I killed Service, it became 540384 ms, and the last time I killed Service, it became 2161536ms.

So, now I know why my Service does not restart after repeated killing. I still don’t know why the Facebook service reboots very quickly every time it is killed (without increasing the restart time). Any ideas on how to solve this problem?

 05-08 09:52:20.997: I/ActivityManager(192): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=ie.cathalcoffey.android/.MyActivity} from pid 3364 05-08 09:52:21.403: I/ActivityManager(192): Displayed ie.cathalcoffey.android/.MyActivity: +290ms (total +41s557ms) 05-08 09:53:01.888: I/ActivityManager(192): Killing proc 3330:ie.cathalcoffey.android/10033: kill background 05-08 09:53:01.888: W/ActivityManager(192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 135096ms 05-08 09:55:17.013: I/ActivityManager(192): Start proc ie.cathalcoffey.android for service ie.cathalcoffey.android/.MyService: pid=3633 uid=10033 gids={3003} 05-08 09:56:21.325: I/ActivityManager(192): Killing proc 3633:ie.cathalcoffey.android/10033: kill background 05-08 09:56:21.333: W/ActivityManager(192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 540384ms 05-08 10:05:21.747: I/ActivityManager(192): Start proc ie.cathalcoffey.android for service ie.cathalcoffey.android/.MyService: pid=3943 uid=10033 gids={3003} 05-08 10:17:20.786: I/ActivityManager(192): Killing proc 3943:ie.cathalcoffey.android/10033: kill background 05-08 10:17:20.786: W/ActivityManager(192): Scheduling restart of crashed service ie.cathalcoffey.android/.MyService in 2161536ms 

Ok, so filtering LogCat with facebook returns the following interesting information, again interesting material is shown in bold. How do they do it? How to include this no-kill logic in my service.

 05-08 10:47:15.896: I/ActivityManager(192): Killing proc 4530:com.facebook.katana/10077: kill background 05-08 10:47:15.896: W/ActivityManager(192): Scheduling restart of crashed service com.facebook.katana/.service.MediaUploadService in 24955ms 05-08 10:47:16.552: W/ActivityManager(192): Permission Denial: Accessing service ComponentInfo{com.facebook.katana/com.facebook.katana.service.MediaUploadService } from pid=907, uid=10069 that is not exported from uid 10077 05-08 10:47:16.560: W/System.err(907): java.lang.SecurityException: Not allowed to stop service Intent { cmp=com.facebook.katana/.service.MediaUploadService } 05-08 10:47:17.263: W/System.err(907): java.lang.SecurityException: Not allowed to stop service Intent { cmp=com.facebook.katana/.service.MediaUploadService } 05-08 10:47:17.263: W/ActivityManager(192): Permission Denial: Accessing service ComponentInfo{com.facebook.katana/com.facebook.katana.service.MediaUploadService} from pid=907, uid=10069 that is not exported from uid 10077 05-08 10:47:17.778: W/System.err(907): java.lang.SecurityException: Not allowed to stop service Intent { cmp=com.facebook.katana/.service.MediaUploadService } 05-08 10:47:17.778: W/ActivityManager(192): Permission Denial: Accessing service ComponentInfo{com.facebook.katana/com.facebook.katana.service.MediaUploadService} from pid=907, uid=10069 that is not exported from uid 10077 
+4
source share
1 answer

The solution was to add exportped = "false" to the service in AndroidManifest.xml

+5
source

Source: https://habr.com/ru/post/1411194/


All Articles