启用/禁用移动数据(GPRS)使用code对升级Froyo 2.2 [英] Enable/Disable Mobile Data (GPRS) using code on Froyo 2.2

查看:218
本文介绍了启用/禁用移动数据(GPRS)使用code对升级Froyo 2.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启用/禁用移动数据联接。
我已经通过的 rIHaN JiTHiN (<一用这个code href=\"http://stackoverflow.com/questions/13523396/enable-disable-mobile-data-gprs-using-$c$c\">Enable/Disable移动数据使用code (GPRS)),它是完美的作品在Android 4.0,但它不会对我的Galaxy S(升级Froyo 2.2)...

I'm trying to Enable / Disable Mobile Data Connexion. I've used this code by rIHaN JiTHiN (Enable/Disable Mobile Data (GPRS) using code) and it's works perfectly on Android 4.0, but it's doesn't on my Galaxy S (Froyo 2.2)...

有没有一种方法,使编程/禁用数据联接?

Is there a way to enable / disable data connexion programmatically ?

如果任何人有任何想法,为什么它是在升级Froyo不工作,将是非常有益的。据 rIHaN JiTHiN ,这code适用于所有Android版本...

If anyone had any idea why it's doesn't work on Froyo, would be really helpful. According to rIHaN JiTHiN, this code works on all Android version...

推荐答案

您可以检查是否启用或通过使用以下code禁用

You can check whether it is enabled or disabled by using below code

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (mMobile.isConnected()) {
    //if internet connected
}

如果它被禁用,您可以使用这一个启用的升级Froyo 设备

if it is disabled, you can enable it on your froyo device by using this one

void turnData(boolean ON) throws Exception
{

if(bv == Build.VERSION_CODES.FROYO)
{

    Log.i("version:", "Found Froyo");
    try{ 
        Method dataConnSwitchmethod;
        Class telephonyManagerClass;
        Object ITelephonyStub;
        Class ITelephonyClass;
        TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

        telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
    Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
    getITelephonyMethod.setAccessible(true);
    ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
    ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

    if (ON) {
         dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); 

    } else {
        dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
    }
    dataConnSwitchmethod.setAccessible(true);
    dataConnSwitchmethod.invoke(ITelephonyStub);
    }catch(Exception e){
          Log.e("Error:",e.toString());
    }

}
 else
{
   Log.i("version:", "Found Gingerbread+");
   final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().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, ON);
}

,也不要忘了这些内容添加到清单

android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_NETWORK_STATE
android.permission.MODIFY_PHONE_STATE
android.permission.READ_PHONE_STATE

这篇关于启用/禁用移动数据(GPRS)使用code对升级Froyo 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆