2012-12-01

Android Service cannot restart after being killed

I have built a simple Android application to schedule mobile data :)
Everything works perfectly... However in my extended testing I found a very weird issue. Sometimes my application service is crashing and it couldn't restart.

Edited to include LogCat logs

...
12-01 00:11:33.083: I/ActivityManager(146): Force stopping package com.mybilet.android16 uid=10096
12-01 00:11:33.093: I/PackageManager(146): Removing non-system package:com.goksu.myApp
12-01 00:11:33.093: I/Process(146): Sending signal. PID: 2840 SIG: 9
12-01 00:11:33.093: I/ActivityManager(146): Force stopping package com.goksu.myApp uid=10107
12-01 00:11:33.093: W/ActivityManager(146): Scheduling restart of crashed service com.goksu.myApp/.service.myAppService in 44738ms
12-01 00:11:33.093: I/ActivityManager(146):   Force stopping service ServiceRecord{40c24020 com.goksu.myApp/.service.myAppService}
12-01 00:11:33.113: I/PackageManager(146): Removing non-system package:com.chartcross.gpstestplus
12-01 00:11:33.113: I/PackageManager(146): Removing non-system package:com.estrongs.android.taskmanager
...
...
12-01 00:12:48.637: D/VoldCmdListener(69): asec mount com.pozitron.biletix-1 {} 1000
12-01 00:12:48.747: D/VoldCmdListener(69): asec path com.pozitron.biletix-1
12-01 00:12:48.817: I/PackageManager(146): /mnt/asec/com.pozitron.biletix-1/pkg.apk changed; collecting certs
12-01 00:12:49.338: D/dalvikvm(146): GC_CONCURRENT freed 1643K, 41% free 9527K/16007K, external 711K/1112K, paused 8ms+19ms
12-01 00:12:49.558: I/PackageManager(146): Linking native library dir for /mnt/asec/com.pozitron.biletix-1/pkg.apk
12-01 00:12:49.588: D/VoldCmdListener(69): asec mount com.goksu.myApp-1 {} 1000
12-01 00:12:49.698: D/VoldCmdListener(69): asec path com.goksu.myApp-1
12-01 00:12:49.748: I/PackageManager(146): Linking native library dir for /mnt/asec/com.goksu.myApp-1/pkg.apk
12-01 00:12:49.758: D/VoldCmdListener(69): asec mount finarea.MobileVoip-1 {} 1000
12-01 00:12:49.938: D/VoldCmdListener(69): asec path finarea.MobileVoip-1
...


Fix:
Well, you can fix this problem when you override onStartCommand method on your service class (like below).
More information: http://developer.android.com/reference/android/app/Service.html


 

package com.goksu.xxx.service;

import android.app.Service;
import android.content.Intent;

/**
 * @author kgoksu
 */
public class MyService extends Service {
 ...
 ...
 @Override
 public void onStart(Intent intent, int startid) {
  handleCommand();
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  handleCommand();
  
     // We want this service to continue running until it is explicitly
     // stopped, so return sticky.
     return START_STICKY;
 }
 
 private void handleCommand() {
  //TODO
 }
 ...
 ...
}










No comments: