I followed some instructions on how to start the service at boot.
In Android 2.2, everything works fine.
I noticed that in Android 2.3 the process crashes and the ActivityManager plans to restart the service again and again.
In my service I want to doSomething() every 5 seconds! For this, I use TimerTask.
Here is the MyService.java code:
package example.service; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private static final String TAG = "MyService"; private static final int TIMER_SECONDS = 5; private Timer doSomethingTimer; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, TAG + ": My Service Created"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, TAG + ": My Service Destroyed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); initDoSomethingTimer(); Log.d(TAG, TAG + ": My Service Started"); return START_STICKY; } private void initDoSomethingTimer() { doSomethingTimer = new Timer(); doSomethingTimer.schedule(new TimerTask() { @Override public void run() { doSomething(); } }, 0, TIMER_SECONDS * 1000); } private void doSomething() { Log.d(TAG, TAG + ": did something!!"); } }
The following is the code for MyStartupIntentReceiver.java :
package example.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyStartupIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(); serviceIntent.setAction("example.service.MyService"); context.startService(serviceIntent); } }
and finally my AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".MyService" > <intent-filter> <action android:name="example.service.MyService" /> </intent-filter> </service> <receiver android:name=".MyStartupIntentReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> </manifest>
The result is not known :( After loading, the service is created, but does not start , and then the ActivityManager schedule crashes to restart it - and start the loop! Here's what we see in Logcat :
I/ActivityManager( 187): Start proc example.service for service example.service/.MyService: pid=615 uid=10052 gids={} D/MyService( 615): MyService: My Service Created (...) I/Process ( 187): Sending signal. PID: 615 SIG: 9 W/ActivityManager( 187): Scheduling restart of crashed service example.service/.MyService in 59628ms (...) I/ActivityManager( 187): Start proc example.service for service example.service/.MyService: pid=639 uid=10052 gids={} D/MyService( 639): MyService: My Service Created (...) I/Process ( 187): Sending signal. PID: 639 SIG: 9 W/ActivityManager( 187): Scheduling restart of crashed service example.service/.MyService in 238512ms
Any suggestions?! I am stuck. I noticed that other services besides this new one (without timertask) started crashing in Android 2.3 with the same errors.