2014-04-08 46 views
0

我正在開發Android應用程序,主要在互聯網上運行。現在的問題是當我使用我的應用程序,由於互聯網應用程序崩潰。事情是在妻子接通之後,我編寫了使用連接管理器類來獲取網絡狀態的代碼,它賦予網絡良好並返回一個真正的價值,但實際上通過網絡的數據沒有處理(這意味着沒有互聯網訪問)。所以,當網絡狀態爲true並且網絡訪問爲假(無法訪問Internet)時,如何防止應用程序崩潰消息。檢查如何停止與網絡連接時崩潰的android應用程序?

+0

沒有嘗試添加'嘗試...趕上'塊? –

+2

打開妻子並沒有幫助..嘗試打開wifi ... – Vikram

+0

yup.not resloved – ponduri

回答

2

嘗試,如果設備連接或不

ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE); 
    boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting(); 
    boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting(); 
    //check Internet connection 
    if(internet||wifi) 
    { 
     //your code 
    }else{ 
     new AlertDialog.Builder(this) 
     .setIcon(R.drawable.title) 
     .setTitle("No internet connection") 
     .setMessage("Please turn on mobile data") 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //code for exit 
       Intent intent = new Intent(Intent.ACTION_MAIN); 
       intent.addCategory(Intent.CATEGORY_HOME);    
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 
      } 

     }) 
     .show(); 
    } 

不要忘了添加所需的權限

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+0

這檢查移動互聯網或wifi是否打開,但沒有提供有關真實連接的信息...例如,wifi可能是但供應商禁用互聯網訪問 –

+0

爲此,您需要創建自定義功能 –

+0

您會指導編寫自定義功能嗎? – ponduri

0

嘗試此代碼之前使用互聯網從您的應用程序:

//Check if Internet Network is active 
private boolean checkNetwork() { 
boolean wifiDataAvailable = false; 
boolean mobileDataAvailable = false; 
ConnectivityManager conManager = (ConnectivityManager)  
getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo(); 
for (NetworkInfo netInfo : networkInfo) { 
if (netInfo.getTypeName().equalsIgnoreCase("WIFI")) 
    if (netInfo.isConnected()) 
    wifiDataAvailable = true; 
     if (netInfo.getTypeName().equalsIgnoreCase("MOBILE")) 
     if (netInfo.isConnected()) 
     mobileDataAvailable = true; 
} 
    return wifiDataAvailable || mobileDataAvailable; 
} 

請將您的代碼放在try catch塊中以查看異常。

來源:http://code2care.org/pages/how-to-check-internet-connection-wifi-3g-4g-is-active-on-android-programatically/

+0

請告訴解決方案上網不能用於網絡連接狀態 – ponduri

0

讓一個類ConnectionDetector

public class ConnectionDetector { 

private Context _context; 

public ConnectionDetector(Context context) { 
    this._context = context; 
} 


public boolean isConnectingToInternet() { 
    ConnectivityManager connectivity = (ConnectivityManager) _context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity != null) { 

     NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
     if (info != null) 
      for (int i = 0; i < info.length; i++) 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) { 

        return true; 
       } 

    } 

    return false; 
} 
    } 
在活動

現在調用它的onCreate()方法

ConnectionDetector cd = new ConnectionDetector(this); 

if(cd.isConnectingToInternet) { 

    // do your stuff here 

} else { 
    // No Internet Connection 
} 
+0

我的問題是沒有檢測到連接狀態。問題在於網絡訪問。感謝您的評論 – ponduri

0
//define global variable 

    public static boolean isHaveInternet = true; 

    //add permission in androidmainfest file 
    <receiver android:name="NetworkReciver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
     </intent-filter> 
    </receiver> 


    //add class for networkcheck 

    package com.example; 
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.util.Log; 

    public class NetworkReciver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent arg1) { 

    boolean status = GeneralClass.getConnectivityStatusString(context); 

    GeneralClass.isHaveInternet = status ? true : false; 
    if (status) 

     Log.e("NET-------------->", "connecte" + status); 

    else 
     Log.e("NET------------->", "not connecte" + status); 
} 
} 

    //check network connection where you want 

    if (GeneralClass.isHaveInternet) 
    { 
        //your code 

    } 
    else 
    { 
        Toast.makeText(getApplicationContext(), 
          "Internet connection not avilable", 
          Toast.LENGTH_SHORT).show(); 
    } 
+0

請告訴解決方案的互聯網訪問不是爲了網絡連接狀態。感謝您的回覆 – ponduri

相關問題