Cathal Coffey
Cathal Coffey

Reputation: 1117

Android Service not restarting after being killed many times in quick succession

I have built a simple Android application to solve an annoying problem. Whenever you join the wireless network at my University you are redirected to a log in page where you must provide your student number and password.

I have build an Android application that lets you enter your details, a service then registers a receiver with the intent filter WifiManager.NETWORK_STATE_CHANGED_ACTION. Whenever the device connected to the University network a HTTP post webrequest is made with the log in details. Thus solving the problem of being redirected.

Everything works perfectly... However in my extended testing I found a very weird issue. When I press the "Go Power Master" applications lightning button once and then open Settings>Apps>Running I see my service is restarting (along with many other services for example Facebook). After about 30 seconds the Services restart and everything is fine once more.

However if I press the lightning button many times (3 or more) in quick succession and then go to Settings>Apps>Running I no longer see my Service. Its like it has never tried to restart. The Facebook Service and other Services go thru the normal restarting phase and eventually succeed.

Does anyone know what is going on? Why is my Service dying forever when I press the lightning button multiple times?

Before you answer know that I have changed my application to work with startForeground() and this works perfectly in all my extended testing. This is however not a valid solution because I don't want my application to have a constant notification in the status bar.

P.S. My project is up on github if you would like to recreate this weird issue https://github.com/ccoffey/NUIMWiFi

P.P.S. The "Go Power Master" application is a free application on Google Play, the lightning button is supposed to reclaim lost RAM? I guess it forces all background Services to die. This is certainly what it appears to do anyway.

Edited to include LogCat logs

Okay I have partially answered my own question. Below is the LogCat output for my Service filtered to only include ie.cathalcoffey.android. I have highlighted the lines that matter. It turns out that each time you kill a Service its restart time increases. Initially it was 135096ms, the second time I killed the Service it became 540384ms and the final time I killed the Service it became 2161536ms.

So now I know why my Service appears to not restart after being killed many times in quick succession. I still don't know why the Facebook Service restarts very quickly every time its killed (no increase in restart time). Any ideas on how to solve this?

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

Okay so filtering LogCat with facebook returns the following interesting information, again the interesting stuff is in bold. How are they doing this? How do I incorporate this no-kill logic into 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

Upvotes: 4

Views: 2713

Answers (1)

Cathal Coffey
Cathal Coffey

Reputation: 1117

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

Upvotes: 5

Related Questions