Reputation: 1182
i am using NetworkStatsManager in 6.0 for getting mobile data and using this method
public long getAllRxBytesMobile1(Context context) {
NetworkStats.Bucket bucket;
try {
bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE,
getSubscriberId(context, ConnectivityManager.TYPE_MOBILE),
0,
System.currentTimeMillis());
} catch (RemoteException e) {
return -1;
}
return bucket.getRxBytes();
}
and
private String getSubscriberId(Context context, int networkType) {
if (ConnectivityManager.TYPE_MOBILE == networkType) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSubscriberId();
}
return "";
}
but returning always zero. how to resolved that?
Upvotes: 2
Views: 1025
Reputation: 1355
Try this:
TelephonyManager telephonyManager = (TelephonyManager) mContext
.getSystemService(Context.TELEPHONY_SERVICE);
int slotIndex = 1;
int subscriptionId = SubscriptionManager.from(mContext).getActiveSubscriptionInfoForSimSlotIndex(slotIndex).getSubscriptionId();
try {
Class c = Class.forName("android.telephony.TelephonyManager");
Method m = c.getMethod("getSubscriberId", new Class[] {int.class});
Object o = m.invoke(telephonyManager, new Object[]{subscriptionId});
String subscriberId = (String) o;
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1