4

我正在为 Android 6.0 制作应用程序,我想使用新类 NetworkStatsManager 来获取移动数据使用情况。

我在清单中添加了我需要的所有权限并需要权限运行时。

当我调用该方法时:

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

返回正确的 WIFI 使用值。

但是如果我用 TYPE_MOBILE 替换 TYPE_WIFI,结果总是 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

3 回答 3

8

我找到了一个隐藏 API 的解决方案(android statistic 3g traffic for each APP,how?),当尝试使用 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 时,您必须使用有效的订阅者 ID。

于 2016-05-31T18:58:04.857 回答
0

作为已接受答案的后续行动,我还想指出,在以下代码中,

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用于获取数据使用情况的信息stats,SIM 1 信息将没有用,因为它不用于消费数据。因此,您需要以某种方式获取TelephonyManagerSIM 2 的信息,以便您可以使用 SIM 2 的正确订户 IDnetworkStatsManager.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 卡的 提供给 来获取每张 SIM 卡的数据使用subscriberId信息networkStatsManager.querySummaryForDevice()。但是,如果您想获得正确的移动数据消耗(包括 SIM 1 和 SIM 2)并且您想支持 Nougat 以下的手机,那么您可能必须使用课堂上的getMobileRxBytes()getMobileTxBytes()方法。TrafficStats

于 2019-04-28T15:34:02.383 回答
0

对接受的答案和@Nandan 的另一个跟进,可以使用以下代码获取两张 SIM 卡的订户 ID

SubscriptionManager subman = (SubscriptionManager)getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE);
TelephonyManager telman = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);

List<SubscriptionInfo> li = subman.getActiveSubscriptionInfoList();

for(SubscriptionInfo info : li){
    int sid = info.getSubscriptionId(); //not the id we want

    try{
        Class c = Class.forName("android.telephony.TelephonyManager");
        Method m = c.getMethod("getSubscriberId", new Class[]{int.class});
        Object o = m.invoke(telman, new Object[]{sid});

        String subid = (String)o;  //id we want
        Log.i("SubscriptionId", subid);
    }
    catch(Exception e){}

}

信用回答链接https://stackoverflow.com/a/38071705/9038181

于 2020-01-08T08:06:20.643 回答