我正在为Android6.0制作一个应用程序,我想使用新的类NetworkStatsManager来获得移动数据的使用。
我在清单中添加了所需的所有权限,并要求权限运行时。
当我调用该方法时:
bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI, "", fromDate.getTime(), toDate.getTime());返回正确的WIFI使用值。
但是,如果我将TYPE_WIFI替换为TYPE_MOBILE,结果总是为0。
NetworkStats.Bucket bucket = null;
try {
bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE, "", fromDate.getTime(), toDate.getTime());
if(bucket == null){
Log.i("Info", "Error");
}else{
Log.i("Info", "Total: " + (bucket.getRxBytes() + bucket.getTxBytes()));
}
} catch (RemoteException e) {
e.printStackTrace();
}发布于 2016-05-31 18:58:04
我用隐藏API (android statistic 3g traffic for each APP, how?)找到了这个问题的解决方案,当我试图用TYPE_MOBILE检索移动数据使用信息时,通知SubscriberID是必要的,这与我尝试获取信息类型WIFI时不同。
试试这段代码
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String subscriberID = tm.getSubscriberId();
NetworkStats networkStatsByApp = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE, subscriberID, start, end, uid);因此,在使用TYPE_MOBILE时,必须使用有效的subscriberID。
发布于 2019-04-28 15:34:02
作为对已接受的答案的后续,我还要指出,在下面的代码中,
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String subscriberID = tm.getSubscriberId(); //subscriberID is usually the IMSI number (for GSM phones)TelephonyManager tm包含有关用于打电话的默认电话服务(SIM卡)的信息。因此,如果您使用带有双SIM卡的电话,而SIM 1用于呼叫,而SIM 2用于数据,那么TelephonyManager tm将保存有关SIM 1的信息,在您的用例中,您正在使用NetworkStatsManager获取数据使用统计数据,SIM 1 info将没有用处,因为它不用于消费数据。因此,您需要为SIM 2获取TelephonyManager,以便在networkStatsManager.querySummaryForDevice()中使用SIM 2的正确用户Id来获取移动数据使用统计数据。那么,你是怎么做到的?
我想出的一种方法是这样:
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);
}
}
}注意,我使用了createForSubscriptionId()方法。这种方法的一个限制是它只能从API 24 (Android7.0)中使用。
因此,如果您同时使用SIM 1和SIM 2进行数据消费,那么您可以通过向subscriberId提供每个SIM卡的数据使用信息来获取它们的数据使用信息。但是,如果您希望获得正确的移动数据消耗(包括SIM 1和SIM 2),并且希望支持努格特以下的电话,那么您可能必须使用TrafficStats类中的好的旧的getMobileTxBytes()方法。
发布于 2020-01-08 08:06:20
在接受答案和@南丹之后,可以使用下面的代码获取两个SIM卡的用户ids
SubscriptionManager subman = (SubscriptionManager)getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE);
TelephonyManager telman = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
List<SubscriptionInfo> li = subman.getActiveSubscriptionInfoList();
for(SubscriptionInfo info : li){
int sid = info.getSubscriptionId(); //not the id we want
try{
Class c = Class.forName("android.telephony.TelephonyManager");
Method m = c.getMethod("getSubscriberId", new Class[]{int.class});
Object o = m.invoke(telman, new Object[]{sid});
String subid = (String)o; //id we want
Log.i("SubscriptionId", subid);
}
catch(Exception e){}
}https://stackoverflow.com/questions/37320399
复制相似问题