Andrei
Andrei

Reputation: 1005

Android Service calling itself again 1 minute later

I'm trying to make a Service, wake up and call itself again after one minute (in this example, I know its bad for battery).

Here is part of the code:

public class SpeechService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();

        setUpNextAlarm();
    }

    public void setUpNextAlarm(){
        Intent intent = new Intent(SpeechService.this, this.getClass());
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

        long currentTimeMillis = System.currentTimeMillis();
        long nextUpdateTimeMillis = currentTimeMillis + 1 * DateUtils.MINUTE_IN_MILLIS;

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)ContextManager.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP,nextUpdateTimeMillis, pendingIntent);
        Log.e("test","I am back!");
    }

    protected void onHandleIntent(Intent intent)
    {
        Log.e("test","I am back!");
        setUpNextAlarm();
    }
}

As you can see I'm calling setUpNextAlarm on service create, I see the log at the end, but then the service is never being called again. I have tried this in an IndentService, it works but I need it to work in a normal Service :(.

Thank you

Upvotes: 0

Views: 947

Answers (2)

Andrei
Andrei

Reputation: 1005

I just ended up using a Service and an IntentService. The IntentService was using the AlarmManager and then it was calling the Service.

Upvotes: 0

nandeesh
nandeesh

Reputation: 24820

Use

 PendingIntent.getService

not

 PendingIntent.getBroadcast

You are getting a Broadcast Intent.

Upvotes: 1

Related Questions