As a follow up to the accepted answer, I would also like to point out that in the following code,
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String subscriberID = tm.getSubscriberId(); //subscriberID is usually the IMSI number (for GSM phones)
TelephonyManager tm
contains the information about the default telephony service (SIM card) that is being used for making calls. So, if you are using a phone with Dual SIM cards and SIM 1 is being used for calls and SIM 2 is being used for Data, then TelephonyManager tm
will hold information about SIM 1 and in your use case, where you are using NetworkStatsManager
for getting Data usage stats, SIM 1 info will be of no use as it is not being used for consuming Data. So, you somehow need to fetch the TelephonyManager
for SIM 2 so that you can use the correct subscriber Id of SIM 2 in networkStatsManager.querySummaryForDevice()
to get the mobile data usage stats. So, how do you do this?
One way that I figured out is like this:
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);
}
}
}
Notice that I have used createForSubscriptionId()
method. One limitation of this method is that it can be used from API 24 (Android 7.0 Nougat) only.
So, if you are using both SIM 1 and SIM 2 for data consumption, then you can get the data usage info for each of the SIM cards by providing the subscriberId
of each of them to networkStatsManager.querySummaryForDevice()
. But, if you want to get correct mobile data consumption (both SIM 1 and SIM 2 inclusive) and you want to support phones below Nougat, then the good old getMobileRxBytes()
and getMobileTxBytes()
methods in TrafficStats
class is what you may have to use.