gpt4 book ai didi

android - StopService onDestroy 被调用但服务未结束

转载 作者:行者123 更新时间:2023-11-29 01:32:59 25 4
gpt4 key购买 nike

我尝试使用 TimerTask 从服务器 api 下载数据,任务在后台应用程序中循环。

我有一个服务类:

public class RequestNewInvoice extends Service {

private Timer timer;
private CheckTask checkTask;
private Handler handler;
private JSONParser jsonParser;
private JSONObject jsonObject;
private boolean startService = true;

private void init(){
this.handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.e("Message: ",msg.obj+"");
return false;
}
});

this.timer = new Timer();
this.checkTask = new CheckTask();
this.startService = true;
timer.scheduleAtFixedRate(checkTask, 5000, 20 * 1000);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
init();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.e("SERVICE STOP", "true");
startService = false;
checkTask.cancel();
timer.cancel();
timer.purge();
}

private class CheckTask extends TimerTask{
@Override
public void run() {
funcCheckInvoice();
}
}

public void funcCheckInvoice(){
try{
if(startService){
Log.e("SERVICE START", "true");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, SuperVAR.GET_CURRENT_SHIPMENT_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SuperVAR.GET_CURRENT_SHIPMENT_TIMEOUT);

httpClient = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(SuperVAR.URL_GET_LIST_INVOICE_IN_CURRENT_SHIPMENT+"?"+ URLEncodedUtils.format(_REQUEST, "utf-8")+"unused="+System.currentTimeMillis()/1000);

httpGet.setHeader("token",token);
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Cache-Control","no-store");

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
jsonObject = jsonParser.getJSONFromRESPONSE(httpEntity);


if(jsonObject == null){
Message message = Message.obtain();
message.obj = "get null.... internet like shit T_T";
handler.sendMessage(message);
}else {
Message message = Message.obtain();
message.obj = "download ok";
handler.sendMessage(message);
}
}else {
Log.e("SERVICE START", "false");
return;
}
}catch (Exception e){
e.printStackTrace();
}
}
}

当我在 Activity 类中运行 stopService 时,服务会调用 onDestroy 函数,但 Timer 仍然存在。

如何销毁定时器任务?

最佳答案

使用 Handler 并在销毁时移除回调任务

    TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();

scanTask = new TimerTask() {
public void run() {
handler.post(task);
}};
t.schedule(scanTask, 5000, 20 * 1000);

Runnable task = new Runnable() {
@Override
public void run() {
//START YOUR JOB
(new FetchData()).execute();

}
};





@Override
public void onDestroy() {
super.onDestroy();
Log.e("SERVICE STOP", "true");
startService = false;
checkTask.cancel();
timer.cancel();
timer.purge();
handler.removeCallbacks(task);

}

public class FetchData extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
funcCheckInvoice();
}
}

关于android - StopService onDestroy 被调用但服务未结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com