gpt4 book ai didi

android - 禁用移动数据

转载 作者:行者123 更新时间:2023-11-29 16:12:12 25 4
gpt4 key购买 nike

我需要以编程方式禁用移动数据连接

我关注了这个

private void setMobileDataEnabled(Context context, boolean enabled) {
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);
}

StackOverFlow Link

但我得到一个错误“java.lang.NullPointerException:Println 需要一条消息”

日志

09-05 18:20:29.081: E/AndroidRuntime(736): FATAL EXCEPTION: Timer-0
09-05 18:20:29.081: E/AndroidRuntime(736): java.lang.NullPointerException: println needs a message
09-05 18:20:29.081: E/AndroidRuntime(736): at android.util.Log.println_native(Native Method)
09-05 18:20:29.081: E/AndroidRuntime(736): at android.util.Log.d(Log.java:138)
09-05 18:20:29.081: E/AndroidRuntime(736): at com.anand.batterysaver.BatterySaverActivity.checkScreenStatus(BatterySaverActivity.java:69)
09-05 18:20:29.081: E/AndroidRuntime(736): at com.anand.batterysaver.BatterySaverActivity$1.run(BatterySaverActivity.java:43)
09-05 18:20:29.081: E/AndroidRuntime(736): at java.util.Timer$TimerImpl.run(Timer.java:284)

检查屏幕状态函数

 public void checkScreenStatus(){
PowerManager powermanager;
GetProcessList list = new GetProcessList();
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

powermanager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (powermanager.isScreenOn() == false)
{
Log.d("PowerStatus","Lock All Settings");
boolean gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
if(gprsstatus == true){
try{
list.setDataEnabled(getApplicationContext(), false);
}catch (Exception e) {Log.d("Exception",e.getMessage()); }
}
}else{
Log.d("PowerStatus","Resume All");
gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
if(gprsstatus == true){
TelephonyManager gprs = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
if(gprs.getDataState() == TelephonyManager.DATA_CONNECTED){
Log.d("Gprs","on");
}
}
}
}

最佳答案

试试这个:这有效(这是我在市场上的一个应用程序的代码)

private static void updateAPN(Context paramContext, boolean enable) {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) paramContext.getSystemService("connectivity");
Method setMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enable);
} catch (Exception e) {
e.printStackTrace();
}
}

奖金:

private static boolean isAPNEnabled(Context paramContext) {
try {
NetworkInfo networkInfo = ((ConnectivityManager) paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
return networkInfo.isConnected();
} catch (Exception e) {
return false;
}
}

顺便说一下,不要忘记这一点:

uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.READ_PHONE_STATE"

关于android - 禁用移动数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12281020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com