Get mobile data usage in Android 10
Asked Answered
H

3

6

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.

Hippocras answered 4/9, 2019 at 19:17 Comment(0)
V
3

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.

Vaccination answered 9/2, 2020 at 11:17 Comment(1)
Yes, I confirm it worked for me too. I'm using Android 10Synonymous
M
3

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

Mapping answered 15/10, 2019 at 7:55 Comment(1)
It worked in my case. An Android 10 custom ROM based on AOSP.Coenobite
V
3

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.

Vaccination answered 9/2, 2020 at 11:17 Comment(1)
Yes, I confirm it worked for me too. I'm using Android 10Synonymous
S
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.

Subsumption answered 5/9, 2019 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.