重置网络配置 android,重置Android移动网络信号?
发布日期:2021-06-24 16:16:21 浏览次数:3 分类:技术文章

本文共 2670 字,大约阅读时间需要 8 分钟。

棒棒糖支持需要新的系统级别权限android.permission.MODIFY_PHONE_STATE才能工作.

private static boolean setMobileConnectionEnabled(Context context, boolean enabled)

{

try{

// Requires: android.permission.CHANGE_NETWORK_STATE

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD){

// pre-Gingerbread sucks!

final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony");

getITelephony.setAccessible(true);

final Object objITelephony = getITelephony.invoke(telMgr);

final Method toggleDataConnectivity = objITelephony.getClass()

.getDeclaredMethod(enabled ? "enableDataConnectivity" : "disableDataConnectivity");

toggleDataConnectivity.setAccessible(true);

toggleDataConnectivity.invoke(objITelephony);

}

// Requires: android.permission.CHANGE_NETWORK_STATE

else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){

final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

// Gingerbread to KitKat inclusive

final Field serviceField = connMgr.getClass().getDeclaredField("mService");

serviceField.setAccessible(true);

final Object connService = serviceField.get(connMgr);

try{

final Method setMobileDataEnabled = connService.getClass()

.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);

setMobileDataEnabled.setAccessible(true);

setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled));

}

catch(NoSuchMethodException e){

// Support for CyanogenMod 11+

final Method setMobileDataEnabled = connService.getClass()

.getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE);

setMobileDataEnabled.setAccessible(true);

setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));

}

}

// Requires: android.permission.MODIFY_PHONE_STATE (System only, here for completions sake)

else{

// Lollipop and into the Future!

final TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);

setDataEnabled.setAccessible(true);

setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));

}

return true;

}

catch(NoSuchFieldException e){

Log.e(TAG, "setMobileConnectionEnabled", e);

}

catch(IllegalAccessException e){

Log.e(TAG, "setMobileConnectionEnabled", e);

}

catch(IllegalArgumentException e){

Log.e(TAG, "setMobileConnectionEnabled", e);

}

catch(NoSuchMethodException e){

Log.e(TAG, "setMobileConnectionEnabled", e);

}

catch(InvocationTargetException e){

Log.e(TAG, "setMobileConnectionEnabled", e);

}

return false;

}

需要权限.

转载地址:https://blog.csdn.net/weixin_33758343/article/details/117681004 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:android 6.0权限统一,snails-permission
下一篇:android显示多个复选框,android - 在长列表视图中选择特定的复选框会选中多个复选框 - 堆栈内存溢出...

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月31日 03时57分39秒