Android NetworkStatsManager Android Mobile Phone Story

I am creating an application for Android 6.0, and I want to use the new NetworkStatsManager class to use mobile data.

I have added all the necessary permissions to the manifest and require runtime execution.

When I call the method:

bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI, "", fromDate.getTime(), toDate.getTime());

return the correct value to use WIFI.

But if I replace TYPE_WIFI with TYPE_MOBILE, the result will always be 0.

    NetworkStats.Bucket bucket = null;
    try {
        bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE, "", fromDate.getTime(), toDate.getTime());

        if(bucket == null){
            Log.i("Info", "Error");
        }else{
            Log.i("Info", "Total: " + (bucket.getRxBytes() + bucket.getTxBytes()));
        }

    } catch (RemoteException e) {
        e.printStackTrace();
    }
+4
source share
2 answers

API ( Android 3g APP, ?) TYPE_MOBILE SubscriberID, , TYPE WIFI.

    TelephonyManager  tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    String subscriberID = tm.getSubscriberId();

    NetworkStats networkStatsByApp = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE, subscriberID, start, end, uid);

, TYPE_MOBILE, .

+7

,

TelephonyManager  tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String subscriberID = tm.getSubscriberId(); //subscriberID is usually the IMSI number (for GSM phones)

TelephonyManager tm (SIM-), . , SIM-, SIM- 1 , SIM- 2 , TelephonyManager tm SIM- 1 , NetworkStatsManager , SIM 1 , . , - TelephonyManager SIM 2, SIM 2 networkStatsManager.querySummaryForDevice() . , ?

, , :

public void subscriberIdsOfDualSim(){
    SubscriptionManager subscriptionManager = SubscriptionManager.from(this);
    //we'll now get a List of information of active SIM cards
    //for example, if there are 2 SIM slots and both the slots have 1 SIM card each, then the List size will be 2
    List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
    TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
        //loop through the SIM card info
        //if there are 2 SIM cards, then we'll try to print the subscriberId of each of the SIM cards
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            //the following createForSubscriptionId() is available from API 24 :(
            TelephonyManager manager1=manager.createForSubscriptionId(subscriptionInfo.getSubscriptionId());
            String operatorName=manager1.getNetworkOperatorName();
            String subscriberId=manager1.getSubscriberId(); //use this subscriberId to do NetworkStatsManager stuff
            System.out.println("subscriberIdsOfDualSim: Carrier Name: "+operatorName+", subscriber id: "+subscriberId);
        }
    }
}

, createForSubscriptionId(). , API 24 (Android 7.0 Nougat).

, SIM- 1 SIM- 2 , SIM-, networkStatsManager.querySummaryForDevice() subscriberId networkStatsManager.querySummaryForDevice(). ( SIM 1 SIM 2 ) , getMobileRxBytes() getMobileTxBytes() TrafficStats - , TrafficStats .

0

All Articles