12

我正在使用 asyncTask 显示对话框,然后在几分钟后启动一个新活动。

不幸的是,该活动在任务完成之前开始???

package com.android.grad;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.AsyncTask;

import android.widget.Toast;

public class LoginTask extends AsyncTask<Void, Void, Boolean> {
private Activity activity;
private ProgressDialog pd;

public LoginTask(Activity activity) {
    this.activity = activity;
}

@Override
protected void onPreExecute() {
    pd = ProgressDialog.show(activity, "Signing in",
            "Please wait while we are signing you in..");
}

@Override
protected Boolean doInBackground(Void... arg0) {
    try {
        Thread.sleep(10000000);
    } catch (InterruptedException e) {
    }
    pd.dismiss();
    return true;
}

@Override
protected void onPostExecute(Boolean result) {
    Toast.makeText(activity, Boolean.toString(result), Toast.LENGTH_LONG).show();
}

}

我从按钮单击侦听器执行任务:S

private OnClickListener loginOnClick = new OnClickListener() {

        public void onClick(View v) {
            new LoginTask(LoginActivity.this).execute();
            startActivity(new Intent(LoginActivity.this, BuiltInCamera.class));
        }
    };

有没有办法从我的子类 ofAsyncTask 开始活动。

4

4 回答 4

35

是的,您可以从 AsyncTask 的子类开始活动。见下文:

@Override
protected void onPostExecute(Boolean result) {
    Toast.makeText(activity, Boolean.toString(result), Toast.LENGTH_LONG).show();

    activity.startActivity(new Intent(activity, BuiltInCamera.class));
}

进行此更改后,请确保确实从中删除startActivityOnClickListener

于 2012-05-24T12:00:21.023 回答
5

调用startActivity内部onPostExecute方法AsyncTask

于 2012-05-24T11:59:07.367 回答
5

在显示 toast 消息之后调用它startActivity(new Intent(LoginActivity.this, BuiltInCamera.class));onPostExecute()

这样,新的活动将在你AsyncTask结束后被调用。

于 2012-05-24T12:00:51.873 回答
5

你也可以使用

    Intent intent = new Intent(activity, PageViewActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    activity.getApplicationContext().startActivity(intent);
于 2015-03-27T07:18:07.223 回答