1

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

我跟着这个

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 链接

但我收到错误“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");
                }
            }
        }
    }
4

2 回答 2

4

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

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"
于 2012-09-05T12:15:57.290 回答
0

我使用了相同的方法,但是我添加了 throws Exception,如下所示:

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

以这种方式调用了一个方法

    try {
    Tools.setMobileDataEnabled(getApplicationContext(), true); //true for enable, false for disable
    } catch (Exception e) { }
}

它对我有用。

于 2012-09-05T12:18:07.613 回答