2017-03-17 78 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

沒有改變其仍表現出遺憾的是應用程序已停止工作 –