简体   繁体   中英

How to Detect the End of Mobile Data Session on Android Phones

I am looking to log the number of Packet Data Sessions an Android phone on 3G or 2G has in a given set of time.

I was hoping that the TelephonyManager.DATA_DISCONNECTED event in the PhoneStateListeners corresponds to the end of data sesion but it doesn't , as my operator charges me for a session ( the popUp message which comes after data usage ) after multiple DATA_DISCONNECTED events.

You need to use a BroadcastReceiver to listen to the data disconnect event or data connect event. First you need to add the receiver in AndroidManifest.xml .

 <receiver
        android:name=".Networkconncted"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

Next you need create a class which extends BroadcastReceiver , this class would be called when data is connected/disconnected.

public class Networkconncted extends BroadcastReceiver {
    public static final String key = "networkInfo";
    public static final String disconnected = "DISCONNECTED";
    public static final String MOB_INT = "mobile";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String value = extras.get(key).toString();
            Log.v("netappcheck", value);
            String temp[] = value.split(",");
            if (temp[0].toLowerCase().contains(MOB_INT)) //check if mobile n/w
                if (temp[1].contains(disconnected)) {
                    Toast.makeText(context,"data disconnected",Toast.LENGTH_SHORT).show();
                    //do something when data is disconnected
                } else {
                    Toast.makeText(context,"data connected",Toast.LENGTH_SHORT).show();
                    //do something when data is connected
                }
        }
    }
}

When the broadcastreceiver is called the intent.getExtras().get(key) will return the below value:

[type: MOBILE[EDGE], state: DISCONNECTED/DISCONNECTED, 
reason: (unspecified), extra: airtelgprs.com, roaming: false, 
failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]

The field in type mentions the type of network is connected, when connected/disconected wifi type will return WIFI[] , when connected to mobile network(3G,EDGE etc) the type field will return MOBILE[] .

The field in state mentions connectivity state.

The variable temp[] puts the type in 0th position, state in 1st position. We need to check if its MOBILE in 0th position as we need to get only to track the mobile network connectivity.Then check the state in the 1st position.

I tested it in Android 5.0.1 and Android 4.0.4 it works fine. More information here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM