Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Run a Service on Boot in Android 2.3 (Gingerbread) without crashing
    primarykey
    data
    text
    <p>I followed several instructions in how to run a service on boot.</p> <p>In <strong>Android 2.2</strong> everything works OK.</p> <p>I noticed that in <strong>Android 2.3</strong> the process crashes and ActivityManager schedules the service to restart over and over.</p> <p>In my service I want to <code>doSomething()</code> every 5 seconds! For this, I'm using a TimerTask.</p> <p>Here is <strong>MyService.java</strong> code:</p> <pre><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!!"); } } </code></pre> <p>Here is <strong>MyStartupIntentReceiver.java</strong> code:</p> <pre><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); } } </code></pre> <p>and, finnally my <strong>AndroidManifest.xml</strong> file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="example.service" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;service android:name=".MyService" &gt; &lt;intent-filter&gt; &lt;action android:name="example.service.MyService" /&gt; &lt;/intent-filter&gt; &lt;/service&gt; &lt;receiver android:name=".MyStartupIntentReceiver" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The result is not famous :( After the boot, the service is created, but <strong>not started</strong>, and then crashes and ActivityManager schedule to restart it - and start the loop! Here is what we can see at <strong>Logcat</strong>:</p> <pre><code>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 </code></pre> <p>Any suggestions?! I'm stuck. I noticed that other services besides this new one (without the timertask) started to crash in <strong>Android 2.3</strong>, with the same errors.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload