2

I want to create a service, that runs a method after 10 seconds over and over again, until I stop it, even if the app is closed. My attempted code is below

package com.example.tauben;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class Reminder extends Service {


    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        TimerTask task = new TimerTask() {
            public void run(){
                f();
            }
        };

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, 10000);
    }

    public void f(){
        Toast t = Toast.makeText(this, "Service is still running", Toast.LENGTH_SHORT);
        t.show();
   }

}

And this is how I start the service.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(Log_TAG,"_____________RESTART__________");

    Intent i= new Intent(this, Reminder.class);
    startService(i); 
}

The program just stops running immediately; how can I change my code to get the desired behaviour?

Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
bsv
  • 49
  • 1
  • 7

1 Answers1

3

create a broadcast receiver that will start your service after receiving broadcast from AlarmManager :

public class SyncAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent data) {

    // set alarm to start service
    Calendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY,  calendar.get(Calendar.HOUR_OF_DAY));

    // start service after an hour
    cal.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 60);
    cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
    cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));

    // set alarm to start service again after receiving broadcast
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, SyncAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    am.cancel(pendingIntent);
    am.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);

    intent = new Intent(context, Reminder.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(intent);
    }
}

start your broadcast receiver for the first time when you start your application:

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, SyncAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    am.cancel(pendingIntent);
    am.set(AlarmManager.RTC, System.currentTimeMillis(), pendingIntent);

Also add this to your Manifest File:

<receiver android:name=".SyncAlarm" >
</receiver>

You should also have alook at this START_STICKY if you want your service to be started by Android system as soon as resources are available instead of receiving Broadcast by AlarmManager.

Ankit Popli
  • 2,809
  • 3
  • 37
  • 61
  • Thanks. I'm gonna try it this afternoon. – bsv Oct 28 '13 at 15:37
  • I dont really understand the code. I think it would be very helpful for me if you could tell me where i have to put witch code. – bsv Oct 28 '13 at 18:48
  • SyncAlaram is reusable class. just add it to your project. Add the 2nd block of code inside your `MainActivity` and the final block of code inside Manifest file. Here's a nice tutorial http://www.vogella.com/articles/AndroidServices/article.html that'll get you started with services incase you didn't get any of this. :) – Ankit Popli Oct 28 '13 at 19:14
  • and this one to get you started with broadcast receivers: http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html – Ankit Popli Oct 28 '13 at 19:15
  • Big Thanks. I hope I get it – bsv Oct 28 '13 at 21:14
  • Somehow i didnt get it to work. The methode of the service gets done one time, but i does not repeat. – bsv Oct 28 '13 at 21:37
  • right now its set to 60 minutes.. you should reduce the time for testing purpose.. make sure you have everything updated in manifest file. if it doesn't work just share the logs. – Ankit Popli Oct 29 '13 at 07:25