发布网友 发布时间:2022-11-27 07:18
共1个回答
热心网友 时间:2023-10-15 01:54
123456789101112131415161718192021222324public 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;}1 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />注意: 要使用系统签名, 否则权限不够另特别注意, 有些支持双卡的手机, 在用此代码前要开过数据连接, 不然不知道是开哪张卡