2012-07-06 17 views
0

我想問一個對你們中的一些人來說可能很簡單的問題。Android應用程序找出是否有連接

我怎麼知道我的應用程序是否有互聯網連接?

我想連接到MySQL數據庫,當沒有Internet時,應該有一個AlertDialog。

new AsyncTask() { 
     ProgressDialog dialog = ProgressDialog.show(AppActivity.this, "Lade", "Daten werden abgerufen...", true); 
     @Override 
     protected void onPostExecute(Object result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      dialog.dismiss(); 
      max = Name.size()-1; 
      kontrolle = new boolean [max*2]; 

      pb.setMax(max/2); 

      java.util.Arrays.fill(kontrolle, false); 

      bilder(); 
     } 

     @Override 
     protected Object doInBackground(Object... arg0) 
     { 
      // TODO Auto-generated method stub 
       dialog.show(); 
       getData(); 
      return null; 
     } 
    }.execute(); 

private void getData() 
{ 
    String result = ""; 
    ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 

    try 
    { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost("http://.../read.php"); 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     HttpResponse response = httpClient.execute(httpPost); 
     HttpEntity entity = response.getEntity(); 
     is=entity.getContent(); 
    } 
    catch(Exception e) 
    { 
     Log.e("log-tag","Keine Verbindung"+e.toString()); 
     Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show(); 
    } 

    try 
    { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = ""; 

     while((line= reader.readLine())!=null) 
       { 
        sb.append(line+"n"); 
       } 
     is.close(); 
     result = sb.toString(); 
     result.trim(); 
    } 
    catch(Exception e) 
    { 
     Log.e("log-tag","Error"+e.toString()); 
    } 
    try 
    { 
     JSONArray jArray = new JSONArray(result); 
     for(int i=0;i<jArray.length();i++) 
     { 
      JSONObject json_data = jArray.getJSONObject(i); 


      ID.add((String) json_data.get("id")); 
      Name.add((String) json_data.get("name")); 
      Pts.add((String)json_data.get("pts")); 
     } 
    } 
    catch(Exception e) 
    { 
     Log.e("log-tag","Error2"+e.toString()); 
    } 
} 

我該如何做到這一點?我不知道 我認爲這

catch(Exception e) 
{ 
    Log.e("log-tag","Keine Verbindung"+e.toString()); 
    Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show(); 
} 

會做的工作,但是當我沒有連接(因爲我把我在飛行模式下手機)也僅僅是一個無止境的ProgressDialog :(

+1

重複問題在這裏http://stackoverflow.com/questions/9570237/android-check-internet-connection – 2012-07-06 09:50:32

回答

1

由於喬恩泰勒提到這是一個duplicate question

你可以用下面的代碼來做到這一點

此方法檢查是否移動連接到互聯網,如果連接返回true,並顯示一個警告框,如果沒有:

private boolean isNetworkConnected() { 
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo ni = cm.getActiveNetworkInfo(); 
if (ni == null) { 
// There are no active networks. 
AlertDialog.Builder altDialog= new AlertDialog.Builder(this); 
altDialog.setMessage("No network connection!"); 
return false; 
} else 
return true; 
} 

將其添加到清單文件,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

編輯:爲了讓你可以使用此代碼段的網絡類型:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

//mobile 
State mobile = conMan.getNetworkInfo(0).getState(); 

//wifi 
State wifi = conMan.getNetworkInfo(1).getState(); 

,然後用它像這樣:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) { 
//mobile 
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) { 
//wifi 
} 
+0

謝謝,並對不起 – user1364052 2012-07-06 09:59:53

+0

我可以區分wifi螞蟻3克嗎? – user1364052 2012-07-06 10:01:22

+0

如果編輯我的答案檢查萎凋連接是WiFi或3G,EDGE ... ect – 2012-07-06 10:09:18

相關問題