最新需要在安卓5.1上自己写控制飞行模式的开关,有没有哪位大神会的,之前的方式用不了了

解决方案 »

  1.   

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.telephony.TelephonyManager;
    import android.os.Bundle;
    import android.telephony.TelephonyManager;/* 
    权限和要求:
       <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
       <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
       
        android:sharedUserId="android.uid.system"    Android.mk  add LOCAL_CERTIFICATE := platform       
    */
    public class MobileData{

    //安卓5.0以上 用这种方式
    public void setMobileDataState(Context cxt, boolean mobileDataEnabled) {
            TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
            if (null != setMobileDataEnabledMethod)
            {
                setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
            }
        }
        catch (Exception e) {
          
        }
    }
    public boolean getMobileDataState(Context cxt) {
            TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
            if (null != getMobileDataEnabledMethod)
            {
                boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
                return mobileDataEnabled;
            }
        }
        catch (Exception e) {
        
        }    return false;
    }

    }