Android5.1怎么用代码实现打开数据连接。。。在线急等

2025-03-24 18:47:47
推荐回答(2个)
回答(1):

	public void setMobileDataState(Context context, boolean enabled) {
TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled",boolean.class);
if (null != setDataEnabled) {
setDataEnabled.invoke(telephonyService, enabled);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public boolean getMobileDataState(Context context) {
TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getDataEnabled) {
return (Boolean) getDataEnabled.invoke(telephonyService);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
 

注意: 要使用系统签名, 否则权限不够

另特别注意, 有些支持双卡的手机, 在用此代码前要开过数据连接, 不然不知道是开哪张卡

回答(2):

没戏吧。5.0以后不能直接开数据连接了。