使用Android应用程序类运行线程背景,将一些数据发布到WebService



我可以在Android的应用程序类中编写线程吗?该线程每五分钟运行一次将数据发布到网络服务。

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);
    }
};

当我的应用程序输入背景时,此线程(例如不运行)。由于我在线程中发布的数据最终输入到数据库中,我找不到DB。

为什么?

我在将数据发布到Web服务时编写一些日志。并发现该日志是随机生成的。 非常奇怪

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);

在MyService中是一个服务类,将您的内容写入OnStart,以将请求发送到WebServices。该服务将每5分钟呼吁。

希望这会有所帮助。

谢谢

最新更新