在onPostExecute()之后没有开始的新意图

时间:2017-03-17 19:07:42

标签: java android android-intent android-asynctask

这是一个java文件来做我的后台活动,在onPostExecute()我添加了一个意图启动但它不起作用,这个后台类只是一个java文件,它不是任何活动的java文件

public class background extends AsyncTask<String,Void,String> {

    private ProgressDialog dialog;
    private ConnectivityManager cm;
    String jsonurl, jsonstring;
    mobile_form mform;
    private  Context context;



    background (Context ctx){
        this.context = context.getApplicationContext();
        this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        this.dialog = new ProgressDialog(ctx);
        mform = new mobile_form();
    }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
           context.startActivity(new Intent(context, mobile_form.class));
           NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
            if (isConnected) {
                if (dialog.isShowing())
                    dialog.dismiss();
            }

        }

2 个答案:

答案 0 :(得分:1)

public class background extends AsyncTask<String,Void,String> {

    private ProgressDialog dialog;
    private ConnectivityManager cm;
    String jsonurl, jsonstring;
    mobile_form mform;
    private  Context context;

    public background (Context ctx){
        this.context = ctx;
        this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        this.dialog = new ProgressDialog(ctx);
        mform = new mobile_form();
    }

    //...
    // doInBackground()
    //...

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
           NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
            if (isConnected) {
                if (dialog.isShowing())
                    dialog.dismiss();
            }
            context.startActivity(new Intent(context, mobile_form.class));
        }

使用此AsynkTask:

new background(getApplicationContext()).execute();

答案 1 :(得分:0)

您可以按照以下显示的方式解决问题 公共类背景扩展了AsyncTask {

private ProgressDialog dialog;
private ConnectivityManager cm;
String jsonurl, jsonstring;
mobile_form mform;
private  mContext context;

public background (Context ctx){
    this.mContext = ctx;
    this.cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    this.dialog = new ProgressDialog(ctx);
    mform = new mobile_form();
}

//...
// doInBackground()
//...

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
       NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        if (isConnected) {
            if (dialog.isShowing())
                dialog.dismiss();
        }
        mContext.startActivity(new Intent(mContext, mobile_form.class));
    }

将此后台任务称为

new background(this).execute();