Android 使用 AsyncTask 启动活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10737016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:23  来源:igfitidea点击:

Using AsyncTask to start activity

androidandroid-asynctask

提问by Mahmoud Emam

I am using asyncTask to show Dialog and then after few minutes then launch a new activity.

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

unfortunately that activity start before task finished ???

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

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();
}

}

}

and i execute the task from button click listener :S

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

private OnClickListener loginOnClick = new OnClickListener() {

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

Is there way to startActivity from my subClass ofAsyncTask .

有没有办法从我的 AsyncTask 子类 startActivity 。

回答by waqaslam

Yes, you can start activity from AsyncTask's sub class. See below:

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

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

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

After making this change, make sure you do remove startActivityfrom OnClickListener

进行此更改后,请确保您删除startActivityOnClickListener

回答by Raghu Nagaraju

Call startActivityinside onPostExecutemethod of AsyncTask

调用startActivity内部onPostExecute方法AsyncTask

回答by Shrikant

Call this startActivity(new Intent(LoginActivity.this, BuiltInCamera.class));from onPostExecute()after Displaying toast message.

调用此startActivity(new Intent(LoginActivity.this, BuiltInCamera.class));onPostExecute()显示敬酒消息之后。

In this way, new activity will be called after your AsyncTaskis over.

这样,您AsyncTask结束后将调用新的活动。

回答by Atish Dabholkar

You can also use

你也可以使用

    Intent intent = new Intent(activity, PageViewActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    activity.getApplicationContext().startActivity(intent);