2017-03-17 45 views
0

这是一个java文件做我的背景活动,在onPostExecute()我添加了一些意向开始,但它不工作,这样的背景类只是一个Java文件它不是任何活动的Java文件onPostExecute()后,无法启动新的意图

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

     } 

回答

0

你可能会做如下面展示其中c一个解决您的问题 公共类背景扩展的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(); 
+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(); 
+0

没有改变其仍表现出遗憾的是应用程序已停止工作 –