brpaz
brpaz

Reputation: 3658

android run service in background from time to time when the application is running

I am building an android app and when it is running I need to make a call to the my web server at each minute if the user is connected to a certain network.

I plan to use a service to make that call but how do I call it at each minute?. I think i need to use alaarm manager but where do I initalize it? in my start activity? I only need to execute the service when my app is running.

Thanks for your help.

Upvotes: 1

Views: 2794

Answers (4)

Mohd Mufiz
Mohd Mufiz

Reputation: 2236

If you want to call server only if app is running then no need to use alarm manager. there are other options like

CoundDownTimer

Thread

I prefer CoundownTimer in that scenario and you can use like this

CountDownTimer countDownTimer = new CountDownTimer(1000000, 60 * 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
            // Do something on a tick.
    }

    @Override
    public void onFinish() {
        // Do something, maybe?

        this.start();
    }
};
countDownTimer.start();

Upvotes: 4

prozhyga
prozhyga

Reputation: 439

in Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   TestAlarmMessageReceiver almesr = new TestAlarmMessageReceiver(this, time);
}

BroadcastReceiver:

public class TestAlarmMessageReceiver extends BroadcastReceiver {


    public TestAlarmMessageReceiver() {
    }

    public TestAlarmMessageReceiver(Context context, int timeout) { //timeout in seconds
        AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, TestAlarmMessageReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
                PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());

        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 
                timeout*60*1000, pendingIntent);
    }

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        arg0.startService(new Intent(arg0, TestMessageService.class));
    }

}

Service:

public class TestMessageService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        messageUpdateTask();
        return super.onStartCommand(intent, flags, startId);
    }

    private void messageUpdateTask(){
        GetMessagesUpdateAsyncTak getMessUpd = new GetMessagesUpdateAsyncTak();
        getMessUpd.execute(this);
    }
}

Upvotes: 0

AndroidLearner
AndroidLearner

Reputation: 4636

Try this ::

You can call this timer in your activity where you want it

private Timer autoUpdate;
@Override
public void onResume() {
    super.onResume();
    autoUpdate = new Timer();
    autoUpdate.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    //call your service from here
                }
            });
        }
    }, 0, 60000);//set time interval according to your requirement
}

Feel free to ask if you have any query :)

Upvotes: 1

Venkata Krishna
Venkata Krishna

Reputation: 1603

Call your web server using AsynTask http://developer.android.com/reference/android/os/AsyncTask.html

In onPost method in AsynTask class wait for one minute and call AsynTask.

You can call AsynTask by using below code :

  BLSyncingProcedure objSyncingProcedure=new BLSyncingProcedure();
        objSyncingProcedure.execute(HomeScreen.this);

BLSyncingProcedure is your AsynTask class name.

Upvotes: -1

Related Questions