4

I followed several instructions in how to run a service on boot.

In Android 2.2 everything works OK.

I noticed that in Android 2.3 the process crashes and ActivityManager schedules the service to restart over and over.

In my service I want to doSomething() every 5 seconds! For this, I'm using a TimerTask.

Here is 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!!");
    }

}

Here is MyStartupIntentReceiver.java code:

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, finnally 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 famous :( After the boot, the service is created, but not started, and then crashes and ActivityManager schedule to restart it - and start the loop! Here is what we can see at 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'm stuck. I noticed that other services besides this new one (without the timertask) started to crash in Android 2.3, with the same errors.

Mat Nadrofsky
  • 8,289
  • 8
  • 49
  • 73
Jorge
  • 695
  • 2
  • 9
  • 21

1 Answers1

0

It seems to me that the problem is that you do not have enough memory, thus android kills this service and starts it again all the time. I had this problem when I tried ICS in the emulator. Try to increase RAM for your AVD (parameter "Device RAM size" to 512Mb) in AVD Manager. Write, please, about the results.

Yury
  • 20,618
  • 7
  • 58
  • 86
  • Im not testing in emulator but on a a device with 512Mb RAM, with free system memory available. – Jorge Dec 30 '11 at 14:57