使用Android应用程序类运行一个线程后台发布一些数据web服务 [英] Use android Application class run a thread background to post some data to webservice
问题描述
我可以写在Android的线程应用程序类?
这个线程运行每五分钟POST数据到一个Web服务。
公共类MyApplication的扩展应用{
@覆盖
公共无效的onCreate(){
startUploadGPSTimer();
}私人无效startUploadGPSTimer(){
gpsTimerHandler.postDelayed(可运行,5 * 60 * 1000); //启动定时器
}私人处理程序gpsTimerHandler =新的处理程序();私人Runnable接口可运行=新的Runnable(){
公共无效的run(){
地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(纬度,Global.CUR_LATITUDE);
params.put(经度,Global.CUR_LONGITUDE);
WebServiceObj的obj =新WebServiceObj(上载,
WebServiceMethod.METHOD_UPLOAD_GPS,
Utilly.getSoapParams(PARAMS));
SoapService服务= NULL;
SoapObject结果= NULL;
服务=新SoapService(obj.tag);
结果= service.LoadResult(OBJ);
Log.i(后webservrce,result.toString());
gpsTimerHandler.postDelayed(这一点,5 * 60 * 1000);
}
};
在我的应用程序进入后台,这个线程就像不运行。由于数据的我主题发表最后输入数据库,我找不到它的分贝。
为什么?
我写的一些日志后的数据做webservice时。 ,发现日志中随机产生。
很奇怪的。
意图photosIntent =新意图(这一点,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);
在为MyService是一个服务类写你的东西在的OnStart发送请求给Web服务。这项服务将要求每5分钟。
希望这有助于。
感谢
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
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
这篇关于使用Android应用程序类运行一个线程后台发布一些数据web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!