我试图找出在每个应用程序在Android上的数据使用量。 喜欢的东西, Android的数据使用应用程序和配额/帽监视器小工具:永远不会收取额外的数据或再次获得封顶! 。
我看着堆栈溢出的问题如何去在Android环境检测数据使用 。
但它没有多大帮助。
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo( mInfo );
List<RunningAppProcessInfo> listOfRunningProcess = activityManager.getRunningAppProcesses();
Log.d(TAG, "XXSize: " + listOfRunningProcess.size());
for (RunningAppProcessInfo runningAppProcessInfo : listOfRunningProcess) {
if (runningAppProcessInfo.uid > 1026)
{
Log.d(TAG, "ANS " + runningAppProcessInfo.processName +
" Id :" + runningAppProcessInfo.pid +
" UID: " + runningAppProcessInfo.uid);
}
}
我试图上面的代码作为由锆石阿科什建议 。 但是,所有这些的UID是数字,不像app_79
正如你上面提到。 这是所有的权利?
以下链接可以帮助你找出如何以编程方式确定每个应用程序的数据使用量。
你将需要实现你的代码使用TraficStats API和跟踪发送/每字节UID(应用程序)收到的数量。
使用此方法创建一个新类PackageInformationTotal之后。
public void getPakagesInfoUsingHashMap() {
final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
// loop through the list of installed packages and see if the selected
// app is in the list
for (ApplicationInfo packageInfo : packages) {
// get the UID for the selected app
UID = packageInfo.uid;
String package_name = packageInfo.packageName;
ApplicationInfo app = null;
try {
app = pm.getApplicationInfo(package_name, 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = (String) pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
// internet usage for particular app(sent and received)
double received = (double) TrafficStats.getUidRxBytes(UID)
/ (1024 * 1024);
double send = (double) TrafficStats.getUidTxBytes(UID)
/ (1024 * 1024);
double total = received + send;
if(total>0)
{
PackageInformationTotal pi=new PackageInformationTotal();
pi.name=name;
pi.packageName=package_name;
pi.icon=icon;
pi.totalMB=String.format( "%.2f", total )+" MB";
pi.individual_mb=String.format( "%.2f", total );
totalData+=Double.parseDouble(String.format( "%.2f", total ));
dataHash.add(pi);
Log.e(name,String.format( "%.2f", total )+" MB");
}
}
Editor edit=shared.edit();
edit.putString("Total",String.format( "%.2f", totalData));
edit.commit();
}
之后,你可以跟踪所有MB过程用法。
Prorammatically:
你可以申报的意图过滤器ACTION_MANAGE_NETWORK_USAGE
行动(Android 4.0中介绍)来表明你的应用程序定义,提供了选项来控制数据使用的活动。 ACTION_MANAGE_NETWORK_USAGE
显示了用于管理特定应用的网络数据使用设置。 当你的应用程序有一个设置的活动,允许用户控制网络使用情况,您应该声明该活动这个意图过滤器。 检查了这一点有关管理数据使用情况的详细信息管理每个应用程序的使用 。
的正确定义ACTION_MANAGE_NETWORK_USAGE
是你可以看到在这里 。
public class Main extends Activity {
private Handler mHandler = new Handler();
private long mStartRX = 0;
private long mStartTX = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Uh Oh!");
alert.setMessage("Your device does not support traffic stat monitoring.");
alert.show();
} else {
mHandler.postDelayed(mRunnable, 1000);
}
}
private final Runnable mRunnable = new Runnable() {
public void run() {
TextView RX = (TextView)findViewById(R.id.RX);
TextView TX = (TextView)findViewById(R.id.TX);
long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
RX.setText(Long.toString(rxBytes));
long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
TX.setText(Long.toString(txBytes));
mHandler.postDelayed(mRunnable, 1000);
}
};
}
您还可以检出https://github.com/commonsguy/cw-andtuning/tree/master/TrafficMonitor
这个片段也适用于在您的设备实际上那些正在运行的应用
final PackageManager pm = getPackageManager();
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
//final List<ActivityManager.RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < appProcesses.size(); i++) {
Log.d("Executed app", "Application executed : " + appProcesses.get(i).processName + "\t\t ID: " + appProcesses.get(i).pid + "");
// String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
//String packageName = appProcesses.get(i)..getPackageName();
ApplicationInfo app = null;
try {
app = pm.getApplicationInfo(appProcesses.get(i).processName, 0);
if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
// tx = TrafficStats.getUidTxBytes(app.uid);
//rx = TrafficStats.getUidRxBytes(app.uid);
long delta_rx = TrafficStats.getUidRxBytes(app.uid) - rx;
long delta_tx = TrafficStats.getUidTxBytes(app.uid) - tx;
}
}
经过长期斗争,我能找到了在Android设备上安装的每个应用程序的任何接口获取数据的解决方案。
由于Android提供TrafficStats API,但因为设备引导这些API是为每个应用程序UID COMPLE统计中的数据和偶数的API不支持在特定应用程序的任何界面来获取数据。 即使我们依靠在TraffiucStates的API,我们得到每个应用程序的新数据statstics。
所以我想使用隐藏的API来使用这个..
在这里,我提的步骤获得数据statstics超过Android中任何接口的每个应用程序...
Estabalish一个“INetworkStatsSession”会议
#进口android.net.INetworkStatsSession;
INetworkStatsSession mStatsSession = mStatsService.openSession();
根据要测量interafce创建网络Templeate ..
#import static android.net.NetworkTemplate.buildTemplateEthernet; #import static android.net.NetworkTemplate.buildTemplateMobile3gLower; #import static android.net.NetworkTemplate.buildTemplateMobile4g; #import static android.net.NetworkTemplate.buildTemplateMobileAll; #import static android.net.NetworkTemplate.buildTemplateWifiWildcard; #import android.net.NetworkTemplate; private NetworkTemplate mTemplate; mTemplate = buildTemplateMobileAll(getActiveSubscriberId(this .getApplicationContext()));
GetActive SubcriberID:
private static String getActiveSubscriberId(Context context) { final TelephonyManager tele = TelephonyManager.from(context); final String actualSubscriberId = tele.getSubscriberId(); return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); }
收集相应的应用程序的网络的tory BYT传递应用程序的UID ...
private NetworkStatsHistory collectHistoryForUid(NetworkTemplate template, int uid, int set) throws RemoteException { final NetworkStatsHistory history = mStatsSession.getHistoryForUid( template, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES); return history; }
得到总消费数据:
public void showConsuption(int UID){ NetworkStatsHistory history = collectHistoryForUid(mTemplate, UID, SET_DEFAULT); Log.i(DEBUG_TAG, "load:::::SET_DEFAULT:.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes())); history = collectHistoryForUid(mTemplate, 10093, SET_FOREGROUND); Log.i(DEBUG_TAG, "load::::SET_FOREGROUND::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes())); history = collectHistoryForUid(mTemplate, 10093, SET_ALL); Log.i(DEBUG_TAG, "load::::SET_ALL::.getTotalBytes:"+ Formatter.formatFileSize(context, history.getTotalBytes())); }