Android:onDestroy()中使用的IntentService

时间:2013-07-01 10:46:52

标签: android intentservice

我试图在用户退出应用程序后删除数据库中的特定注册表。对于那个问题,我调用一个IntentService,当应用程序ID被销毁时,它会以理论方式运行。问题是,虽然此意图不能按预期工作,但注册表不会被删除。如果您愿意,可以使用以下代码。 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();
                    }
            }



        }

未达到日志

主要活动:

public void onDestroy(){
    super.onDestroy();
    Intent msgIntent = new Intent(Titulacion.this, FinIntentService.class);
    startService(msgIntent);

}

我之前尝试过使用Asynctask但是在onDestroy时我无法获得所需的效果。然而,在某些情况下(30个中的1个),synctask完成了它的任务。

1 个答案:

答案 0 :(得分:0)

实际上无法调用onDestroy方法。

  仅当系统资源不足(内存,CPU时间等)时才会调用

onDestroy,并决定终止您的活动/应用程序,或者有人在您的活动上调用finish()。

如果您想要发布一些资源,则应该在 onPause() 中执行此操作。