我有一次应用程序停止应发位置, 一个应用程序我重写的onStop方法和它运行的服务完美 但也达到了当位置发送者什么都没有发生,这里是我的服务代码:异步任务不会触发
@Override
public void onCreate() {
super.onCreate();
dbHelper = new DBHelper(this);
warnniSharedPreference = new WarnniSharedPreference(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocationSender locationSender = new LocationSender();
locationSender.execute();
return android.app.Service.START_NOT_STICKY;
}
private class LocationSender extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (null != response) {
dbHelper.deleteAllRecords(DBHelper.LOCATION_TRACKER_TABLE_NAME);
dbHelper.deleteAllRecords(DBHelper.IMAGES_TABLE_NAME);
}
}
@Override
protected Void doInBackground(Void... voids) {
try {
int start = 0;
int end = 0;
ArrayList<WarnniLocation> locationArrayList = dbHelper.getAllLocationRecords();
int locationGroupSize = (int) Math.ceil((double) locationArrayList.size()/(double) 60);
for (int i = 0; i < locationGroupSize; i++) {
MultipartUtility multipartUtility = new MultipartUtility(Constants.ADD_LOCATION_DATA, "UTF-8");
multipartUtility.addFilePart("image", new File(imagesPaths.get(i)));
multipartUtility.addFormField("driverId", String.valueOf(warnniSharedPreference.getKey("id")));
if (i == 0) {
end = 59;
} else {
end = i * 60 + 59;
if (end >= locationArrayList.size()) {
end = locationArrayList.size() - 1;
}
}
multipartUtility.addFormField("locations", new Gson().toJson(locationArrayList.subList(start, end)));
response = multipartUtility.finish();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
,请提前通知 感谢和任何帮助将不胜感激
为什么在服务中使用'AsyncTask'?服务已经在后台运行。 – Yupi
那我应该直接发送请求吗? –
我得到了android.os.NetworkOnMainThreadException –