0

我正在開發一個應用程序,更頻繁地向Web服務發出請求。我想要做的是,當互聯網不可用或不連接時,而不是繼續請求;應用程序應顯示一條消息,指示無法使用互聯網並自殺。我有這個代碼工作的罰款與我 -AsyncTask與對話框不可用,然後結束整個應用程序

public GetXMLFromServer(Context context, GetXMLCallback gc, 
      ArrayList<NameValuePair> params, String message) { 
    this.context = context; 
    this.gc = gc; 
    this.postParameters = params; 
    progressDialog = new ProgressDialog(this.context); 
    this.message = message; 
} 

protected void onPreExecute() { 
    progressDialog.setTitle("Please Wait"); 
    progressDialog.setMessage(message); 
    progressDialog.show(); 
} 

@Override 
protected void onPostExecute(String result) { 
    progressDialog.dismiss(); 
    gc.onSuccess(result); 
} 

@Override 
protected String doInBackground(String... params) { 
    String response = null;  
    try{ 
     ConnectivityManager cm = (ConnectivityManager) this.context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) {   
     } else if (netInfo == null) { 
      AlertDialog alertDialog = new AlertDialog.Builder(this.context).create(); 
      alertDialog.setTitle("Connection Problem"); 
      alertDialog.setMessage("You are not connected to Internet"); 
      alertDialog.setButton("Exit", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) {         
          isAvailable=false; 
          return; 
         } 
        }); 
      alertDialog.show(); 
     } 

     /*if(isAvailable==false){ 
      then finish the calling activity or entire app. 
     }*/ 
    }catch(Exception e){ 

    } 
    try { 
     response = CustomHttpClient.executeHttpPost(this.urlToFetch, 
       this.postParameters);   
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return response; 
} 
    }` 

現在我的問題是,我無法弄清楚如何提出請求和崩潰的應用程序之前結束整個應用程序或調用活動,因爲那裏是不可用互聯網。 (註釋部分代碼是我想停止應用程序並結束它的地方)

回答

1

我的第一個問題是,爲什麼不在啓動AsyncTask()之前檢查網絡連接?

鑑於此,如果您仍想將其包含在AsyncTask()中,則需要執行以下操作。你想要做的是取消AsyncTask。有兩個關鍵要做到這一點。

  1. 調用cancel()函數。
  2. 入住適當位置(doInBackground(...)中的AsyncTask isCancelled()之內。如果你被取消,然後儘快結束功能越好。

至於讓用戶知道,我建議你彈出一個對話框,並在對話框中onClick()方法,finish()活動。

這可能是更容易,如果你把你的對話框中的onCreateDialog(),如the documentation所示做到這一點。然後父活動將是應用程序,而不是AsyncTask,因此finish()聲明將b e正確。

+0

(對不起,我的知識貧乏)'取消()評論'是從呼叫活動的'的onDestroy(叫)'方法(我猜)。但是如果互聯網不可用,我無法從對話框的onClick()中完成調用活動,因爲我沒有調用活動的正確上下文。 (也就是說'finish()'也是未定義的)只有在調用'cancel'時,'isCanceled'纔會成立。那麼如何做到這一點? :( – SachinGutte 2012-04-03 15:06:34

+0

增加了更多的細節,必須明確調用cancel(),但是仔細看看代碼,看起來不太合理。但是,更好的方法可能是在啓動AsyncTask()之前檢查網絡的連通性, ' – PearsonArtPhoto 2012-04-03 15:21:15

+0

我想我不能調用finish()的原因是它屬於'extends''Activity'的活動,而不是'extends''AsyncTask'的類。 – SachinGutte 2012-04-03 15:22:30

0

替換

return "someSpecificString"; 

和onPostExecute()

if (result.equals("someSpecificString") 
    MyActivity.this.finish(); 
+0

嘗試它。它現在說'finish()'未定義或將其轉換爲「東西「。 – SachinGutte 2012-04-03 15:10:27

+0

TheNameOfTheActivityWhereTheCodeYouShowedIs.this.finish()does not work? – pouzzler 2012-04-03 15:12:15

+0

不幸的是,它不起作用,我認爲它會起作用:( and one mor事情上面提到的課程是分開的,並且將從各種活動中調用。那些調用活動必須完成。 – SachinGutte 2012-04-03 15:13:52

相關問題