28
Дек
2016

Как программно отключить передачу данных Android

Есть сервис, который в фоне проверяет какой тип сети 4G/3G, и если тип 3G должна отключаться передача данных.

Вот код сервиса.

public class MyService extends Service {

final String LOG_TAG = "myLogs";
private Handler handler;
private String type;

public void onCreate() {
    super.onCreate();
    Log.d(LOG_TAG, "onCreate");
}

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(LOG_TAG, "onStartCommand");
    someTask();
    handler = new Handler();
    return super.onStartCommand(intent, flags, startId);
}

public void onDestroy() {
    super.onDestroy();
    Log.d(LOG_TAG, "onDestroy");
}

public IBinder onBind(Intent intent) {
    Log.d(LOG_TAG, "onBind");
    return null;
}

void someTask() {
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                type = getNetworkClass(getBaseContext());
                ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                if (mMobile.isConnected()) {
                    Log.d(LOG_TAG, "------------------------ isConnected");
                    //if internet connected
                }else {
                    Log.d(LOG_TAG, "++++++++++++++++++++++++ isDisConnected");
                }
                if(type.equals("2G")){
                    Log.d(LOG_TAG, "type 2 - " + type);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "!!! Тип сигнала сети " + type, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else {
                    if (type.equals("3G")) {
                        Log.d(LOG_TAG, "type 3 - " + type);

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "!!! Тип сигнала сети " + type, Toast.LENGTH_SHORT).show();
                            }
                        });


                    }else{if(type.equals("4G")){
                        Log.d(LOG_TAG, "type 4 - " + type);
                    }
                    }
                }

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public String getNetworkClass(Context context) {
    TelephonyManager mTelephonyManager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    int networkType = mTelephonyManager.getNetworkType();
    switch (networkType) {
        case TelephonyManager.NETWORK_TYPE_GPRS:
        case TelephonyManager.NETWORK_TYPE_EDGE:
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_1xRTT:
        case TelephonyManager.NETWORK_TYPE_IDEN:
            return "2G";
        case TelephonyManager.NETWORK_TYPE_UMTS:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_HSDPA:
        case TelephonyManager.NETWORK_TYPE_HSUPA:
        case TelephonyManager.NETWORK_TYPE_HSPA:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return "3G";
        case TelephonyManager.NETWORK_TYPE_LTE:
            return "4G";
        default:
            return "Unknown";
    }
}

}

Пробовал так, не работает

final ConnectivityManager conman =
            (ConnectivityManager) getBaseContext().getSystemService(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.class);

    setMobileDataEnabledMethod.setAccessible(true);

    // (true) to enable 3G; (false) to disable it.
    setMobileDataEnabledMethod.invoke(iConnectivityManager, false);

Источник: https://ru.stackoverflow.com/questions/608751/%D0%9A%D0%B0%D0%BA-%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%BD%D0%BE-%D0%BE%D1%82%D0%BA%D0%BB%D1%8E%D1%87%D0%B8%D1%82%D1%8C-%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D1%87%D1%83-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85-android

Share

Тебе может это понравится...