我試圖檢查收到短信的互聯網連接。 但是我的服務是在更改互聯網狀態時強制關閉。例如,如果我已啓用無線網絡它的工作正常,但當我禁用WiFi應用程序成爲關閉錯誤。 我在onReceive函數中使用此代碼。 這是代碼。在android中檢查互聯網連接
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if(isConnected == true) {
Toast.makeText(context, "Internet", Toast.LENGTH_LONG).show();
}
else if(isConnected == false){
Toast.makeText(context, "No Internet", Toast.LENGTH_LONG).show();
}
http://www.androidhive.info/2012/07/android-detect-internet-connection-status/ –