Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background process timer on android

I'm trying to get a process timer to run and keep it running in the background on android (starts with a button click).

The timer must be on 30 seconds and should even continue growing application in the background (with home button and power / screen off).

How can I do this? I tried with service and handler but not working ...

EDIT

My service tracking (process with 30 sec)

public class TrackingService extends IntentService {

    private Handler mHandler;
    private Runnable mRunnable;

    public TrackingService() {

        super("TrackingService");

    }

    public TrackingService(String name) {

        super(name);

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        long timer = 30000;

        mHandler = new Handler();
        mRunnable = new Runnable() {

            @Override
            public void run() {

                    //TODO - process with update timer for new 30 sec

                    mHandler.postDelayed(this, timer);

            }
        };

        mHandler.postDelayed(mRunnable, timer);

    }

}

My click button:

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //TODO - start first time and it continued every 30 seconds and continue in the background
        startService(Intent intent = new Intent(this, TrackingService.class));

    }
});
like image 344
VTR2015 Avatar asked Dec 10 '22 18:12

VTR2015


1 Answers

Ok, first of all, I really don't know if I got your question quite right. But I think you want a timer that's being executed every 30 seconds ,if i'm not mistaken. If so, do as following:

AlarmManager

Note: This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Example:

in your onClick() register your timer:

int repeatTime = 30;  //Repeat alarm time in seconds
AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, processTimerReceiver.class);   
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Repeat alarm every second
processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent); 

And your processTimerReceiver class:

//This is called every second (depends on repeatTime)
public class processTimerReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //Do something every 30 seconds
    }
}

Don't forget to register your receiver in your Manifest.XML

<receiver android:name="processTimer" >
   <intent-filter>
       <action android:name="processTimerReceiver" >
       </action>
   </intent-filter>
</receiver>

If you ever want to cancel the alarm: use this to do so:

//Cancel the alarm
Intent intent = new Intent(this, processTimerReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

Hope this helps you out.

PS: if this is not exactly what u want, please leave it in the comments, or if someone wants to edit this, please do so.

like image 146
Strider Avatar answered Jan 20 '23 14:01

Strider