4

I'm trying to make a network usage monitor app, which shows mobile data usage history to the user. For this I'm using Usage access to get accurate data usage stats from NetworkStatsManager. But this no longer works in Android 10.

I'm using NetworkStatsManager.querySummaryForDevice which requires subscriber Id, which earlier I was able to obtain using TelephonyManager.getSubscriberId.

But the getSubscriberId is now not working in Android 10 as it requires READ_PRIVILEGED_PHONE_STATE which third-party apps cannot have.

Any ideas on how to make it work? I understand the restrictions for getting subscriber Id, but I don't really care about the subscriber Id as long as I get the mobile data usage, for which I have enough permissions.

4

3 回答 3

3

Try passing null as subscriber ID in the querySummaryForDevice method .. That worked for me

于 2019-10-15T07:55:23.523 回答
2

When calling NetworkStatsManager resolve subscriberId as follows:

  • use null when running on Android versions 10+ (API Level >= 29) devices
  • on prior versions of Android (API Level < 29) you should still resolve subscriberId (using TelephonyManager)

Here is a sample code that should help:

public static String getSubscriberId() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return telephonyManager.getSubscriberId();
        } else {
            return null;
        }
    }

Worked for me on Android API Level 29 and Android API Level 26 devices.

于 2020-02-09T11:17:46.030 回答
0

There's no way at the moment to get the information you want unless your app is a profile or device owner app. You can just use the TrafficStats but you can't use a query and it resets on reboot.

于 2019-09-05T20:11:27.150 回答