问题
I am trying to delete a especific registry in a database once the user quits the app. For that issue I call an IntentService whichs runs theorically when the application id destroyed. The problem is that, although this intent does not work as intended and the registry is not deleted. Here is the code if you may help. IntentService:
public class FinIntentService extends IntentService{
String levelstring = "22";
int pid;
static String pids;
BroadcastReceiver batteryReceiver = null;
String url = "10.0.0.13";
private static String url_crear = "http://10.0.0.13/subida/create_candidato.php";
private static final String url_delete = "http://10.0.0.13/subida/delete_candidato.php";
JSONParser jsonParser = new JSONParser();
@SuppressLint("NewApi")
static String device_id = Build.SERIAL;
static String PDA = device_id.substring(device_id.length() - 6);
public FinIntentService() {
super("FinIntentService");
}
@Override
protected void onHandleIntent(Intent intent)
{
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", Titulacion.getPID()));
JSONObject json = jsonParser.makeHttpRequest(
url_delete, "POST", params);
Log.d("Delete Product", json.toString());
success = json.getInt("success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
The Log for example is not reached
Main Activity:
public void onDestroy(){
super.onDestroy();
Intent msgIntent = new Intent(Titulacion.this, FinIntentService.class);
startService(msgIntent);
}
I tried to use Asynctask before but I could not get the desired effect when onDestroy. however, in some cases (1 out of 30) the synctask did its task.
回答1:
Actually onDestroy method can not be called.
onDestroy is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.
If you want to release some resources, you should do that in onPause() instead.
来源:https://stackoverflow.com/questions/17402636/android-intentservice-used-in-ondestroy