我可以使用android.settings.WIRELESS_SETTINGS
打开无线和蓝牙,但我应该使用什么来打开移动互联网?
答案 0 :(得分:5)
从Lollipop开始,以编程方式启用移动数据存在限制。
使用此代码来实现移动数据使用,您可以在其中打开/关闭移动数据:
{{1}}
答案 1 :(得分: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;
}
使用up方法后,如果你看到互联网没有连接,那么使用下面的方法打开移动数据
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>
答案 2 :(得分: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;
}`
然后如果status == 0;然后开始这个意图。
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));