【问题标题】:new intent not getting started after onPostExecute()在 onPostExecute() 之后没有开始新的意图
【发布时间】:2017-08-09 11:25:33
【问题描述】:

这是一个用于执行我的后台活动的 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();
            }

        }

【问题讨论】:

    标签: java android android-intent android-asynctask


    【解决方案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();
    

    【讨论】:

    • 没有改变它仍然显示不幸的是应用程序已停止工作
    【解决方案2】:

    您可以按照以下方式解决您的问题 公共类背景扩展 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();
    

    【讨论】:

      猜你喜欢
      • 2019-06-05
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2016-06-11
      相关资源
      最近更新 更多