4

I am writing and testing an android app where I need to restart app on multiple crashes. The restart code works fine, but after 2 crashes the service does not restart, and I see following message in the logs

06-13 02:31:02.098: W/ActivityManager(2117): Service crashed 2 times, stopping: ServiceRecord{42b5b6a0

Looks like service restart after 2 crashes is not being allowed by AcitivityManager.

Is there some API using which we can change this number from 2 to something else ?

A sample app to reproduce this is available here

https://github.com/devendram/servicecrashdemo

Logcat output available here

https://gist.github.com/devendram/5c5259d3f91fd8409e7b#file-gistfile1-txt-L1754

devendram
  • 257
  • 1
  • 5

2 Answers2

0

So I can't swear this applies, but it's one of the better examples of a "ServiceRecord" i found online:

Removed Dead link to hi-android.info, which now advertises suspicious investment products


Also

http://justanapplication.wordpress.com/2012/06/06/dgc_18/

Has some good discussions of the ServiceRecord. From what I'm seen it looks like Android tracks your service, and in order to avoid a number of issues, stops allowing the service to start if it crashes repeatedly. The constants I see don't quite match 2, but I suspect that to be the problem.

Can you find a way to avoid the crashes? Try-Catch for example? This is costly but it would avoid the crashing and colliding....

Otherwise - why is your service crashing so frequently? Why does it need to?

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41
  • 1
    I need to demo recovery ability of the service, hence trying to get it restarted everytime there is a crash. Try catch is not an option because we want the app to crash and restart – devendram Jun 12 '14 at 20:20
  • Ok, well can you provide more information? How are you restarting it? Are you using return START_STICKY in your binder? Or are you manually restarting? – Nathaniel D. Waggoner Jun 12 '14 at 20:30
  • I tried START_STICKY earlier but now I am restarting in exception handler. I added that default exception handler code to the question. – devendram Jun 12 '14 at 20:32
  • I see that. Did you have this issue with start_sticky? – Nathaniel D. Waggoner Jun 12 '14 at 20:32
  • I had some other issues with START_STICKY unrelated to this. I need to check if START_STICKY gives this issue as well. I'll update the info as comment here . – devendram Jun 12 '14 at 20:45
  • What were those issues? Start_sticky is the built in way to handle this situation. – Nathaniel D. Waggoner Jun 12 '14 at 20:53
  • 1
    Just checked with START_STICKY. I hit the same issue. After two crashes, service does not start . 06-13 02:31:02.098: W/ActivityManager(2117): Service crashed 2 times, stopping: ServiceRecord{42b5b6a0 – devendram Jun 12 '14 at 21:04
  • Can you post a "mininum reproducible code" example that I can work with? – Nathaniel D. Waggoner Jun 12 '14 at 21:13
0

After looking into the android source code, I found an attribute of ProcessRecord which may solve the problem, isPersistent(). Then I dumped the package info of all packages with adb. The result looked as following:

Package [com.android.phone] (fa7158b):                           
  userId=1001                                                    
  sharedUser=SharedUserSetting{3eab065 android.uid.phone/1001}   
  pkg=Package{4db66c com.android.phone}                          
  codePath=/system/priv-app/TeleService                          
  resourcePath=/system/priv-app/TeleService                      
  legacyNativeLibraryDir=/system/priv-app/TeleService/lib        
  primaryCpuAbi=null                                             
  secondaryCpuAbi=null                                           
  versionCode=29 minSdk=29 targetSdk=29                          
  versionName=10                                                 
  splits=[base]                                                  
  apkSigningVersion=3                                            
  applicationInfo=ApplicationInfo{58dac1b com.android.phone}     
  flags=[ SYSTEM HAS_CODE PERSISTENT ALLOW_CLEAR_USER_DATA ]     

The flag PERSISTENT was interesting and I thought it might be set in AndroidManifest.xml. So I checked all available attributes of tag application, and finally got the attribute android:persistent to solve this problem.
By the way, I'm an android system service programmer who writes android applications signed with platform key. I'm not sure if it works also for normal ones.

lichi
  • 21
  • 4