2010-10-25 78 views
0

Hai Friends, 我想向用戶顯示敬酒信息,當網絡連接很差或沒有連接時,所以我在我的活動中寫下了以下代碼。需要幫助檢查Android中的Internet連接?

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

if (

conMgr.getNetworkInfo(0).getState()== NetworkInfo.State.CONNECTED || conMgr.getNetworkInfo(1).getState()== NetworkInfo.State.CONNECTING){

 //notify user you are online 

    }  else if (

conMgr.getNetworkInfo(0).getState()== NetworkInfo.State.DISCONNECTED || conMgr.getNetworkInfo(1).getState()== NetworkInfo.State.DISCONNECTED){// 通知用戶您不在線

 Toast.makeText(getBaseContext(),"Please 

檢查您的Internet連接,並嘗試 再次」,Toast.LENGTH_SHORT) .show();

} 

在登錄錯誤消息是

10-25 15:52:03.412: ERROR/AndroidRuntime(19084): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10031 nor current process has android.permission.ACCESS_NETWORK_STATE. 
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):  at android.os.Parcel.readException(Parcel.java:1218) 
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):  at android.os.Parcel.readException(Parcel.java:1206) 
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):  at android.net.IConnectivityManager$Stub$Proxy.getNetworkInfo(IConnectivityManager.java:266) 
10-25 15:52:03.412: ERROR/AndroidRuntime(19084):  at android.net.ConnectivityManager.getNetworkInfo(ConnectivityManager.java:153) 

回答

3

添加android.permission.ACCESS_NETWORK_STATE許可請求您的應用程序清單:

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

@ Mr.Veeti:u能請告訴我如何檢測互連差,監守我如果淨斷開僅 – 2010-10-25 10:41:53

+2

@Tilsan採寫:定義互聯互通不佳 – Kennet 2010-10-25 12:16:50

+0

@Kennet:請告訴我一點解釋,如何定義互聯網連接不好 – 2010-10-25 12:20:55