2012-10-15 39 views

回答

7

請嘗試下面的代碼,

public class ConnectionAvailable extends BroadcastReceiver { 

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

    ConnectivityManager manager = (ConnectivityManager) context 
      .getSystemService(context.CONNECTIVITY_SERVICE); 
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) 
      .isConnectedOrConnecting(); 
    boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) 
      .isConnectedOrConnecting(); 

    if (!is3g && !isWifi) { 

     Toast.makeText(context, "Internet Connection Lost", 
       Toast.LENGTH_LONG).show(); 

    } else { 
     if ((intent.getAction() != null) 
       && (intent.getAction() 
         .equals("android.intent.action.AIRPLANE_MODE"))) { 
      Toast.makeText(context, "Internet Connection Lost", 
        Toast.LENGTH_LONG).show();    
     } 
    } 
} 
} 

這可能會幫助你..

+0

s老兄這工作很好 – Vicky

+0

nJoy !!!!!!!!!! :) –

1

試試下面的代碼:

private void setMobileDataEnabled(Context context, boolean enabled) { 
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
final Class conmanClass = Class.forName(conman.getClass().getName()); 
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); 
iConnectivityManagerField.setAccessible(true); 
final Object iConnectivityManager = iConnectivityManagerField.get(conman); 
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); 
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); 
setMobileDataEnabledMethod.setAccessible(true); 

setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled); 
} 
+0

我想檢查時,應用程序將減少 – Vicky

1
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
+0

我想檢查時,應用程序將減少 – Vicky

+0

你的意思是你想,當應用程序在後臺運行檢查? – Syn3sthete

+0

s正是我需要檢查背景 – Vicky

1
public static boolean isNetworkAvailable(Context context) { 
      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; 
      } 
1
public boolean isOnline() { 
    ConnectivityManager cm = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false;} 
    and add this permission  

調用此方法,並在添加此權限您清單文件<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

+0

我知道檢查網絡狀態時,應用程序將運行我的問題是如何檢測應用程序將盡量減少網絡狀態(後臺程序) – Vicky