简体   繁体   English

如何在Android手机上检测移动数据会话的结束

[英]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. 我希望记录给定时间内3G或2G上的Android手机具有的分组数据会话数。

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. 我希望PhoneStateListeners中的TelephonyManager.DATA_DISCONNECTED事件与数据会话的结束相对应,但是不希望如此,因为我的操作员向我收取了多个DATA_DISCONNECTED事件的会话( 数据使用弹出消息 )的费用。

You need to use a BroadcastReceiver to listen to the data disconnect event or data connect event. 您需要使用BroadcastReceiver来侦听数据断开事件或数据连接事件。 First you need to add the receiver in AndroidManifest.xml . 首先,您需要在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. 接下来,您需要创建一个扩展BroadcastReceiver的类,当连接/断开数据时将调用该类。

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: 调用广播接收器时, intent.getExtras().get(key)将返回以下值:

[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[] . 类型字段表示连接的网络类型,当连接/断开连接的wifi类型将返回WIFI[] ,当连接到移动网络(3G,EDGE等)时,类型字段将返回MOBILE[]

The field in state mentions connectivity state. 状态字段提到连接状态。

The variable temp[] puts the type in 0th position, state in 1st position. 变量temp []将类型置于第0位,状态置于第1位。 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. 我们需要检查其MOBILE在第0位,因为我们只需要跟踪移动网络的连通性即可。然后检查第1位的状态。

I tested it in Android 5.0.1 and Android 4.0.4 it works fine. 我在Android 5.0.1和Android 4.0.4中对其进行了测试,效果很好。 More information here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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