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