从AsyncTask开始另一个活动

时间:2014-08-15 12:43:41

标签: android android-activity android-fragments

我想知道如何在完成任务后开始一项活动,详情如下:

在我的班级,我有AsyncTask,完成任务后,我想拨打activity2

   private class AccessTokenGet extends AsyncTask<String, String, Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Boolean doInBackground(String... args) {

            return true;
        }

        @Override
        protected void onPostExecute(Boolean response) {
            if (response) {
            //call activity2
        }
    }

2 个答案:

答案 0 :(得分:2)

如果你的班级是内在的

   public class StackQuestion extends Activity {

        @Override
        protected void onCreate( final Bundle savedInstanceState ) {
            super.onCreate( savedInstanceState );

            final MyAsyn mMyAsyn = new MyAsyn( this );
            mMyAsyn.execute();
        }

        public class MyAsyn extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground( final Void ... params ) {
                return null;
            }

            @Override
            protected void onPostExecute( final Void result ) {
                super.onPostExecute( result );

                startActivity( new Intent( "your intent here" ) );
            }
        }
    }

如果它在一个单独的文件中

public class StackQuestion extends Activity {

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        final MyAsyn mMyAsyn = new MyAsyn( this );
        mMyAsyn.execute();
    }
}

public class MyAsyn extends AsyncTask<Void, Void, Void> {

    private final Activity mActivity;

    public MyAsyn( final Activity mActivity ) {
        this.mActivity = mActivity;
    }

    @Override
    protected Void doInBackground( final Void ... params ) {
        return null;
    }

    @Override
    protected void onPostExecute( final Void result ) {
        super.onPostExecute( result );

        this.mActivity.startActivity( new Intent( "your intent here" ) );
    }
}

<强>编辑:

public class Activity1 extends Activity {

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        final AccessTokenGet mAccessTokenGet = new AccessTokenGet( this );
        mAccessTokenGet.execute();
    }
}

public class Activity2 extends Activity {

    @Override
    protected void onCreate( final Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );

        //Your code here
    }
}

public class AccessTokenGet extends AsyncTask<String, String, Boolean> {

    private final Activity mActivity;

    public AccessTokenGet( final Activity mActivity ) {
        this.mActivity = mActivity;
    }

    @Override
    protected Boolean doInBackground( final String ... args ) {

        return true;
    }

    @Override
    protected void onPostExecute( final Boolean response ) {

        if ( response ) {
            this.mActivity.startActivity( new Intent( this.mActivity.getBaseContext(), Activity2.class ) );
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

答案 1 :(得分:0)

@Override 
protected void onPostExecute(Boolean response) {
    if (response) {
        progress.hide(); 

        final Intent intent = new Intent(this, Activity2.class);
        activity1.startActivity(intent);

    } 
} 

这里唯一的问题是你获得对activity1的引用 - 这里有一些选项取决于你的项目 - 例如您可以将它传递给构造函数中的异步任务。

PS:小心这个uncehcked progress.hide(); - 当有人在此

之前将您的活动发送到后台时(例如通过按回家),这可能会崩溃