我需要以编程方式禁用移动数据连接
我遵循了这个
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)
checkScreenStatus函数
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/