get the status of 3G/2G data - Android Phone

Android examples for Phone:Network

Description

get the status of 3G/2G data

Demo Code


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Main{
    /**/* ww  w .  j  a v  a  2s .co  m*/
     * get the status of 3G/2G data
     * 
     * @param context
     * @return true if 3g/2g radio open, false if 3g/2g radio close
     * @throws ClassNotFoundException 
     * @throws NoSuchFieldException 
     * @throws SecurityException 
     * @throws IllegalAccessException 
     * @throws IllegalArgumentException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     */
    public static boolean getMobileDataStatus(Context context)
            throws MobileDataException {
        try {

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> conMgrClass;
            conMgrClass = Class.forName(connManager.getClass().getName());
            Field iConMgrField = conMgrClass.getDeclaredField("mService");
            iConMgrField.setAccessible(true);
            Object iConMgr = iConMgrField.get(connManager);
            Class<?> iConMgrClass = Class.forName(iConMgr.getClass()
                    .getName());
            Method getMobileDataEnabledMethod = iConMgrClass
                    .getDeclaredMethod("getMobileDataEnabled");
            getMobileDataEnabledMethod.setAccessible(true);
            return (Boolean) getMobileDataEnabledMethod.invoke(iConMgr);
        } catch (ClassNotFoundException e) {
            throw new MobileDataException(e);
        } catch (NoSuchFieldException e) {
            throw new MobileDataException(e);
        } catch (IllegalArgumentException e) {
            throw new MobileDataException(e);
        } catch (IllegalAccessException e) {
            throw new MobileDataException(e);
        } catch (NoSuchMethodException e) {
            throw new MobileDataException(e);
        } catch (InvocationTargetException e) {
            throw new MobileDataException(e);
        }
    }
}

Related Tutorials