1

30 分钟后,垃圾收集器杀死或重新启动(startCommand 函数与 START_STICKY 一起)服务,然后服务不再工作,此问题仅在 android 2.3.* 操作系统中发生。

30 分钟后,操作系统再次创建服务,但是这个新创建的服务并没有完成初始服务应该做的事情

public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);

startForeground(0, null);



sessionId = null;
app_version = null;

if(intent != null){

    extras = intent.getExtras();
    if(extras != null){
        sessionId = extras.getString("SESSION_ID");
        app_version= extras.getString("app_version");

        if(sessionId != null && app_version != null && !sessionId.equals("") && !app_version.equals("")){
            SharedPreferences prefs = getSharedPreferences("POSITION_CACHE",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("sessionId", sessionId);
            editor.putString("app_version", app_version);
            editor.commit();
        }
    }
}


SharedPreferences prefs = getSharedPreferences("POSITION_CACHE",Context.MODE_PRIVATE);
sessionId = prefs.getString("sessionId", null);
app_version = prefs.getString("app_version", null);

if(sessionId != null && !sessionId.equals("") && app_version != null && !app_version.equals("")){
    timer();
}
else{
    logout();
}

// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
 }



   private void timer(){
updateTask = new TimerTask() {
    @Override
    public void run() {
        if(checkInternetConnection()){

            try{

            if (currentBestLocation != null && currentStatus==1) {


                    WebServiceConnector ws = new WebServiceConnector(sessionId,PositionService.this.getApplicationContext());
                    ws.pushPosition(currentBestLocation.getLatitude(), currentBestLocation.getLongitude() ,app_version);
                    ws.close();
                }
            }catch(Exception e){

            }
        }

    }
};

try{
    positionTimer = new Timer("TaxiPositionTimer");
    positionTimer.schedule(updateTask, 1000L, updatePeriod * 1000L);
}catch(Exception e){
    timer();
}

}

4

0 回答 0