1

是否有可能在android的广播接收器中使用意图找到互联网启用请帮助我?当应用程序未打开时,我想要确切的后台进程我想检测网络状态

4

5 回答 5

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();               
        }
    }
}
}

这可能会帮助你..

于 2012-10-15T05:56:25.973 回答
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);
 }
于 2012-10-15T05:32:33.040 回答
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;
于 2012-10-15T05:32:34.560 回答
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;
          }
于 2012-10-15T05:32:51.893 回答
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" />

于 2012-10-15T05:34:48.300 回答