android – 使用AsyncTask启动活动

前端之家收集整理的这篇文章主要介绍了android – 使用AsyncTask启动活动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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));
        }
    };

有没有办法从我的subClassAsyncTask启动活动.

解决方法

是的,您可以从AsyncTask的子类开始活动.见下文:
@Override
protected void onPostExecute(Boolean result) {
    Toast.makeText(activity,Toast.LENGTH_LONG).show();

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

进行此更改后,请确保从OnClickListener中删除startActivity

原文链接:https://www.f2er.com/android/310801.html

猜你在找的Android相关文章