如何写一个服务跟踪GPRS数据使用的Andr​​oid设备上 [英] How to write a service to keep track of gprs data usage on android device

查看:263
本文介绍了如何写一个服务跟踪GPRS数据使用的Andr​​oid设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写在android系统服务,将跟踪数据使用的背景。当数据使用的事件将被捕获,我会保存在本地数据库的详细信息。

我读到android系统TrafficStats类但当活动得到启动,它会检查一次。我想一个连续的后台进程运行。

我甚至发现了一个示例项目 -
https://github.com/commonsguy/cw-andtuning/tree/master/TrafficMonitor

在帖子中提到 -
<一href=\"http://stackoverflow.com/questions/8545162/android-how-to-measure-the-data-usage-of-my-app\">Android:如何衡量我的应用程序的数据使用

先谢谢了。


解决方案

您需要做到以下几点:


  • 创建将存储数据的使用值的本地数据库。


  • 启动它运行连续/定期计算/重新计算数据使用的服务。


  • 数据使用由服务计算后,将数据添加到您的数据使用的表。


要创建您可以参考这个教程本地数据库href=\"https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&ved=0CF8QFjAD&url=http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html&ei=ay36UZ29GYLlrAe_3YHQBw&usg=AFQjCNGHrlZI8DewY8QwqR2vRa37N2GJpg&sig2=4ugvnCU-b00VMOu16lfQvw&bvm=bv.50165853,d.bmk\"相对=nofollow> sqlite的

下面是你如何启动服务创建Android中

修改

有没有办法,如果任何新数据的使用是由。你必须定期检查其使用的服务,将连续或定期运行得到通知。

您可以使用以下code来计算用法:

  INT UID = Process.myUid();
长recived = TrafficStats.getUidRxBytes(UID);
长发送= TrafficStats.getUidTxBytes(UID);

,您可以根据您的要求使用其他的功能是:

 长initialRX = TrafficStats.getTotalRxBytes(); //收到
长initialTx = TrafficStats.getTotalTxBytes(); //发送
长initialMobRX = TrafficStats.getMobileRxBytes();
长initialMobTx = TrafficStats.getMobileTxBytes();

记住这是TrafficStats返回累计值.Hence你才能知道增量的使用量,减去initail值

同时的TrafficStats计数器复位,只要这个过程是killed.eg当手机关机。因此,你将不得不增加code来处理它。

相关链接:结果
<一href=\"http://stackoverflow.com/questions/7638292/trafficstats-api-android-and-calculation-of-daily-data-usage\">TrafficStats API的Andr​​oid和日常数据使用计算

I want to write a service in android which will keep track of data usage in the background. when an event of data usage will be captured, I will save the details in local database.

I read about TrafficStats class in android but it will check only once when activity gets started. I want a continuous background process running.

I even found a sample project - https://github.com/commonsguy/cw-andtuning/tree/master/TrafficMonitor

as mentioned in the post - Android: How to measure the data usage of my app

Thanks in advance.

解决方案

You need to do the following :

  • Create a local database that will store the data usage values .

  • Start a service which runs continuously / periodically to calculate /recalculate the data usage .

  • After data usage is calculated by the service ,add the data into your data usage table .

To create local database you can refer to this tutorial on sqlite

Here is how you can start a service Creating a Service in Android

EDIT

There is no way to get notified if any fresh data usage is made .You will have to periodically check it using your service that will run continuously or periodically .

You can use the following code to calculate the usage :

int UID=Process.myUid();
long recived = TrafficStats.getUidRxBytes(UID);
long send = TrafficStats.getUidTxBytes(UID);

Other functions that you can use depending on your requirement are:

long initialRX = TrafficStats.getTotalRxBytes();// recieved
long initialTx = TrafficStats.getTotalTxBytes();// sent
long initialMobRX = TrafficStats.getMobileRxBytes();
long initialMobTx = TrafficStats.getMobileTxBytes();

Remember that that TrafficStats returns a cumulative value .Hence you have to subtract the initail value in order to know the amount of increment in usage

Also The TrafficStats counter is reset whenever the process is killed.eg when phone is shut down . Hence you will have to add code to handle it .

Related Link:
TrafficStats Api android and calculation of daily data usage

这篇关于如何写一个服务跟踪GPRS数据使用的Andr​​oid设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆