Search code examples
androidserviceondestroy

Service that works right for few hours, than no more response


I want to run a method CippaLippa() in the GmailService class when I receive an email in Gmail client.

I've a receiver and a service in AndroidManifest...

    <receiver
        android:name="com.myapp.receiver.GmailReceiver">
        <intent-filter>
            <action
                android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.myapp.service.GmailService"
        android:label="@string/app_name" />

and these classes...

public class GmailReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {


        final SharedPreferences preferences = context.getSharedPreferences("myapp.prefs", 0);

        context.startService(new Intent(context, GmailService.class));
    }
}


public class GmailService extends Service { .. etc...}

My question: everything works right for some hours and when I receive a notification from Gmail, the CippaLippa() method fires... then, after some hours, when I receive a gmail notification, the CippaLippa() method fires no more.

Maybe, there is a way to tell GmailService class to "stay alive" and continue monitoring Gmail events? I think this is not due to Android OS that kills unused classes, because that is a background service and not an Activity. I've no clue.


Solution

  • You're right about the system killing the service. I've had the same problem with a service associated with a widget. I solved it by making it refresh every 30 minutes or so.

    So if you schedule some event once in a while to wake up your service, it should stay up and running.

    There might be a better solution though.