在方法服务OnDestroy()中执行AsyncTask

时间:2015-08-17 13:19:43

标签: android android-asynctask

我有这个问题,我需要运行一个AsyncTask,当它在应用程序中汇集数据的服务被销毁时,但我有以下问题,我可以把代码放在没有AsyncTask的情况下因为它不能做在主线程上,当我在方法onDestroy中执行execute调用时,任务永远不会被执行。 这是onDestroy方法的代码:

@Override
public void onDestroy(){
    new StartPushNotification().execute();
    super.onDestroy();
}

这是我正在尝试执行的任务的代码:

private class StartPushNotification extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // Start IntentService to register this application with GCM.
        InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
        try {
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            String deviceId = instanceID.getId();
            AppManager manager = AppManager.getManager(getApplication());
            Identity identity = manager.RetrieveSharedIdentityObject();
            Boolean res = manager.SaveRegistration(String.valueOf(identity.getIdentityAccountNumber()), identity.getIdentityUser(), identity.getIdentityPassword(), "True", deviceId, "ooo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

注意:我试图调试方法和它执行的OnDestroy方法,但不是AsyncTask。 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试使用此代码而不是AsyncTask,因为无论如何都不需要UI更新。

    @Override
     public void onDestroy() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
                try {
                    String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    String deviceId = instanceID.getId();
                    AppManager manager = AppManager.getManager(getApplication());
                    Identity identity = manager.RetrieveSharedIdentityObject();
                    Boolean res = manager.SaveRegistration(String.valueOf(identity.getIdentityAccountNumber()), identity.getIdentityUser(), identity.getIdentityPassword(), "True", deviceId, "ooo");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        super.onDestroy();
    }

答案 1 :(得分:0)

尝试下面的代码,而不是使用AsyncTask,如codeMagic所建议。

private class MyThread implements Runnable {
    @Override
    public void run() {
        InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
        try {
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            String deviceId = instanceID.getId();
            AppManager manager = AppManager.getManager(getApplication());
            Identity identity = manager.RetrieveSharedIdentityObject();
            Boolean res = manager.SaveRegistration(String.valueOf(identity.getIdentityAccountNumber()), identity.getIdentityUser(), identity.getIdentityPassword(), "True", deviceId, "ooo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onDestroy() {
    new Thread(new MyThread()).start();
    super.onDestroy();
}