-2
         /**
         * Send Button click event.
         **/
        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                      NetAsync(view);

            }
        });
    }

On click event the "NetAsync(view)" execute and send the gps coordinates to server, but what i want is that instead of using button click event when user start the app "NetAsync(view)" execute automatically after every 10 minutes. Please tell me how to do this as i am new to android programming.

Junaid
  • 664
  • 5
  • 18
  • 35
  • Check out this answer - http://stackoverflow.com/questions/3696082/how-to-schedule-my-android-app-to-do-something-every-hour – Aditya Gupta Apr 29 '14 at 04:59
  • Possible to duplicate: http://stackoverflow.com/questions/3538853/how-do-i-run-a-method-every-10-minutes-in-android – Lokesh Apr 29 '14 at 05:00

4 Answers4

17

you can do it using TimerTask and Timer class like this

final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {       
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {       
                        try {
                                                 //your method here
                        } catch (Exception e) {
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 600000); //execute in every 10 minutes
Shahzain ali
  • 1,679
  • 1
  • 20
  • 33
Hamad
  • 5,096
  • 13
  • 37
  • 65
  • but what if i want that it keeps running in background even if user exit the app ?? – Junaid Apr 29 '14 at 07:20
  • 1
    than use this in background service. and if this answer, solved your question,then mark it as an answer. – Hamad Apr 29 '14 at 10:05
1

Try Something like below:

TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();

public void doWifiScan(){

scanTask = new TimerTask() {
        public void run() {
                handler.post(new Runnable() {
                        public void run() {
                        NetAsync(view);
                        }
               });
        }};


    t.schedule(scanTask, 300, 600000); 

 }
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

Using Handler You can easily achieve your goal please follow bellow code,

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            if (!isSyncStart) {
                startSyncProcess(true); //write Your Method Here!
            }
        }
    }, 600000);
bhavesh kaila
  • 761
  • 1
  • 5
  • 25
0

Use AlarmManager with Broadcast Receiver

private PendingIntent mPingAlarmPendIntent;
private static final String PING_ALARM = "com.sithagi.PING_ALARM";
private Intent mPingAlarmIntent = new Intent(PING_ALARM);
private BroadcastReceiver mPingAlarmReceiver = new PingAlarmReceiver();


mPingAlarmPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mPingAlarmIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    // Each and every 15 minutes will trigger onReceive of your BroadcastReceiver   
((AlarmManager)getSystemService(Context.ALARM_SERVICE)).setInexactRepeating(AlarmManager.RTC_WAKEUP, 
        System.currentTimeMillis() + (AlarmManager.INTERVAL_FIFTEEN_MINUTES), AlarmManager.INTERVAL_FIFTEEN_MINUTES, mPingAlarmPendIntent);


// Register the receiver 
registerReceiver(mPingAlarmReceiver, new IntentFilter(PING_ALARM));

// To cancel Alarm Service
((AlarmManager)getSystemService(Context.ALARM_SERVICE)).cancel(mPingAlarmPendIntent);

// unregister the receiver onDestroy or if you want stop
unregisterReceiver(mPingAlarmReceiver);


 /**
 * BroadcastReceiver will trigger 
 */
private class PingAlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context ctx, Intent i) {
        // Do your work here 
    }
}
Chathura Wijesinghe
  • 3,310
  • 3
  • 25
  • 41