-2

How to check in Android that my mobile packet data runs out if I am on 3g network?

Sorry if my question is not very clear. Here is my code for checking 3G connection.

public boolean checking3GConnection()
{
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivity !=null)
    {
        NetworkInfo Info3G = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(Info3G!=null)
        {
            if(Info3G.isConnected())
            {
                return true;
            }
        }
    }
    return false;
}

Actually, when I hit the button(I'm expecting to open the browser to navigate to Google.com) and its calling this method, the problem is because of my 3G plan is run out of data and when I navigated to Google.com, I've seen something like "Out of Credit or Data". I don't want this thing, I'm expecting something that when I hit the button it will check if I still have the 3G data or not before navigate to a website.

Kenny
  • 19
  • 4
  • try catch? You're bound to get an exception of some sort if it cannot connect to 3G – Ben Aug 13 '14 at 09:48
  • Hi Andy, Thanks for your reply. But can you make it little more clear for me? Like, what kind of exception? – Kenny Aug 13 '14 at 09:52
  • I'm not exactly sure because I'm not too familiar with Android but it seems like a general programmatic problem. It would be something like: try{ connectTo3G(); } catch(Exception e) { //handle error how you want } I don't know the specifics of the exception but a look at the documentation will help. – Ben Aug 13 '14 at 09:55
  • Is it possible that you run out of data plan, but can still use your 3g, but get charged, or will you be denied access to the 3g network? – RossC Aug 13 '14 at 10:20

1 Answers1

2

You can use the below class to resolve all the connectivity issues in Android

Nice class posted by someone on Gist

Here is a Gist of the class, so you can fork it and edited it.

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * Check device's network connectivity and speed 
 * @author emil http://stackoverflow.com/users/220710/emil
 *
 */
public class Connectivity {

    /**
     * Get the network info
     * @param context
     * @return
     */
    public static NetworkInfo getNetworkInfo(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo();
    }

    /**
     * Check if there is any connectivity
     * @param context
     * @return
     */
    public static boolean isConnected(Context context){
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected());
    }

    /**
     * Check if there is any connectivity to a Wifi network
     * @param context
     * @param type
     * @return
     */
    public static boolean isConnectedWifi(Context context){
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

    /**
     * Check if there is any connectivity to a mobile network
     * @param context
     * @param type
     * @return
     */
    public static boolean isConnectedMobile(Context context){
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
    }

    /**
     * Check if there is fast connectivity
     * @param context
     * @return
     */
    public static boolean isConnectedFast(Context context){
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
    }

    /**
     * Check if the connection is fast
     * @param type
     * @param subType
     * @return
     */
    public static boolean isConnectionFast(int type, int subType){
        if(type==ConnectivityManager.TYPE_WIFI){
            return true;
        }else if(type==ConnectivityManager.TYPE_MOBILE){
            switch(subType){
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
            /*
             * Above API level 7, make sure to set android:targetSdkVersion 
             * to appropriate level to use these
             */
            case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                return false; // ~25 kbps 
            case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                return true; // ~ 10+ Mbps
            // Unknown
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
            }
        }else{
            return false;
        }
    }

}

Also make sure to add this permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
Dharmendra Pratap Singh
  • 1,332
  • 1
  • 11
  • 22
  • What if he hits his data allowance but stays on the 3g network? That's how my operator works, I've asked the OP to clarify this. – RossC Aug 13 '14 at 10:20
  • 1
    That you need to manage. I just posted a gist that can work as a wrapper to manage network issues. – Dharmendra Pratap Singh Aug 13 '14 at 10:30
  • Exactly, I'm not sure what the OP wants, gist there would be very useful, but beyond that the OP would appear to be asking a `give me teh c0dez` type question (way off topic) or a request for off site resources (off topic also) so it's a tricky one. The question shows no research effort whatsoever. Cheers for posting this though, no doubt it will be useful to others. +1 for it, though it may not fully answer the OP's (dreadful) question. – RossC Aug 13 '14 at 10:39
  • Yeah the data packet tracking still unanswered. – Dharmendra Pratap Singh Aug 13 '14 at 10:45
  • Horrible question, nice answer. That's just how SO goes, I suppose. +1 for the effort and sharing the gist. – 2Dee Sep 30 '14 at 12:42