问题描述
我开发一个Android应用程序,需要下载大量的数据。
我想衡量我的应用程序的时间(如每月)特定时期的数据流量。
此选项可在系统设置 - >数据使用
- 有没有办法通过编程访问此设置?
- 我可以使用一些Android的库来获得流量?
我知道TrafficStats类,但我不能获得流量的特定时期,当我开机的设备数据将丢失。
- 有没有办法通过编程访问此设置?
试试下面这些code:
公共布尔的InvokeMethod(字符串方法名,对象[]参数)抛出异常{
ConnectivityManager MCM =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
类ownerClass = mcm.getClass();
类[] argsClass = NULL;
如果(参数!= NULL){
argsClass =新类[1];
argsClass [0] = args.getClass();
}
方法方法= ownerClass.getMethod(方法名,argsClass);
返程(布尔)method.invoke(MCM,参数);
}
公共对象invokeBooleanArgMethod(字符串方法名,布尔值)抛出异常{
ConnectivityManager MCM =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
类ownerClass = mcm.getClass();
类[] argsClass =新类[1];
argsClass [0] = boolean.class;
方法方法= ownerClass.getMethod(方法名,argsClass);
返回method.invoke(MCM,价值);
}
/ *使用这两种方法,这样的* /
[对象] ARG = NULL;
尝试 {
布尔isMobileDataEnable = invokeMethod中(getMobileDataEnabled,ARG);
如果(!isMobileDataEnable){
invokeBooleanArgMethod(setMobileDataEnabled,真正的);
}
}赶上(例外五){
e.printStackTrace();
}
此外,的AndroidManifest.xml
,你需要添加
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Android:名称=android.permission.CHANGE_NETWORK_STATE/>
您可以设置闹钟或线程服务,定期获得流量与TrafficStats所有过程。如果你想获得每个进程的交通,我觉得你可以看到这个的答案。
I'm developing an Android app which needs to download a lot of data.
I'd like to measure the data traffic of my app for specific period of time (like month).
This option is available on System Settings -> Data Usage.
- Is there any way to access this setting programmatically?
- Can I use some of android libs to get traffic?
I know about TrafficStats class but I can not get traffic for specific period of time and when i boot device this data is lost.
- Is there any way to access this setting programmatically?
try these code below:
public boolean invokeMethod(String methodName, Object[] args) throws Exception {
ConnectivityManager mcm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Class ownerClass = mcm.getClass();
Class[] argsClass = null;
if (args != null) {
argsClass = new Class[1];
argsClass[0] = args.getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return (Boolean)method.invoke(mcm, args);
}
public Object invokeBooleanArgMethod(String methodName, boolean value) throws Exception {
ConnectivityManager mcm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Class ownerClass = mcm.getClass();
Class[] argsClass = new Class[1];
argsClass[0] = boolean.class;
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(mcm, value);
}
/* use these two method like these */
Object[] arg = null;
try {
boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg);
if(!isMobileDataEnable){
invokeBooleanArgMethod("setMobileDataEnabled", true);
}
} catch (Exception e) {
e.printStackTrace();
}
Also, in AndroidManifest.xml
, you need to add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
- Can I use some of android libs to get traffic?
You can set an alarm or thread in service to get traffic periodically with TrafficStats for all process. If you want to get traffic of each process, I think you can see this answer.
这篇关于如何访问特定应用程序的数据使用设置编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!