Query data usage for both sim individually using NetworkStatsManager

448 views Asked by At

I need to query data usage for both the sim cards in dual sim mobile phones using NetworkStatsManager for entire device as need to report 1 day data usage for each sim card. Hence, I need to use the method querySummaryForDevice(). I need to pass subscription id which I believe should be different for each sim. I tried using Subscription Manager solution mentioned here:Android: How to get SIM-ID of both SIMs in Android? Somehow, getting subscription id from here returns 0 bytes as data used. As of now I am using TelephonyManager to get the subscription id.

1

There are 1 answers

2
Deepti On BEST ANSWER
SubscriptionManager subscriptionManager = (SubscriptionManager) ControlApplication.getControlApplicationContext().getSystemService(
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList();
int id = subInfoList.get(i). getSubscriptionId();

//Here, i is index in the list.

Now use reflection to invoke getSubscriberId(int) method in TelephonyManager class with value obtained in above step. Return value for this method will be required subscriber id for the sim which could be used in querySummaryForDevice() method to obtain data usage for that sim. Code using reflection:

Class<?> c = Class.forName("android.telephony.TelephonyManager");
Method m = c.getMethod("getSubscriberId", int.class);
Object o = m.invoke(telephonyManagerInstance, id);
subscriberId = (String) o;