6

可以android.settings.WIRELESS_SETTINGS用来打开无线和蓝牙,但是我应该用什么来打开移动互联网?

4

3 回答 3

7

Lollipop 在以编程方式启用移动数据方面存在限制。

使用此代码意图使用移动数据,您可以在其中打开/关闭移动数据:

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
    startActivity(intent);
于 2016-12-15T06:47:28.417 回答
0

使用此方法(关闭 wifi(如果在 wifi 上))并打开移动数据连接

  public boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean NisConnected = activeNetwork != null && activeNetwork.isConnected();
        if (NisConnected) {
            //  if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                //  wifi.setWifiEnabled(true);//Turn on Wifi
                wifi.setWifiEnabled(false);//Turn off Wifi
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return true;
            else
                return false;
        }
        return false;
    }

用完方法后,如果您看到互联网无法连接,请使用以下方法打开移动数据

 private void setMobileDataEnabled(Context context, boolean enabled) {
        try {
            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);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

也需要清单中的许可

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
于 2015-06-07T05:29:44.120 回答
0

检查是否有互联网。

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }
    return TYPE_NOT_CONNECTED;
}

public static int getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    int status = -1;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = 1;
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = 2;
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = 0;
    }
    return status;
}`

然后如果状态 == 0 ; 然后启动这个 Intent。 startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));

于 2016-12-15T07:23:06.153 回答