2

Could I write a thread in Android the Application class? This thread is running every five minutes to post data to a webservice.

public class MyApplication  extends Application {
@Override
public void onCreate() {
    startUploadGPSTimer();
}

private void startUploadGPSTimer() {
    gpsTimerHandler.postDelayed(runnable, 5* 60 * 1000); // start Timer
}

private Handler gpsTimerHandler = new Handler();

private Runnable runnable = new Runnable() {
    public void run() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("latitude", Global.CUR_LATITUDE);
        params.put("longitude", Global.CUR_LONGITUDE);
        WebServiceObj obj = new WebServiceObj("upload",
                WebServiceMethod.METHOD_UPLOAD_GPS,
                Utilly.getSoapParams(params));
        SoapService service = null;
        SoapObject result = null;
        service = new SoapService(obj.tag);
        result = service.LoadResult(obj);
        Log.i("post webservrce  ", result.toString());
        gpsTimerHandler.postDelayed(this, 5 * 60 * 1000);
    }
};

When my application enter background, this thread like do not running. Because of the data I post in thread Finally input to Database, and I can't find it in db.

Why?

I write some log when do post data to webservice. and found the log be generated randomly. Very strange

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ray
  • 468
  • 4
  • 17

1 Answers1

1
Intent photosIntent = new Intent(this, MyService.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,photosIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 5*60*1000,5*60*1000, pendingIntent);

In MyService is a Service Class write your stuff in onstart to send request to webservices. This service will call for every 5minutes.

Hope this helps.

Thanks

Chaitu
  • 907
  • 2
  • 13
  • 27