How To Write A Service To Keep Track Of Gprs Data Usage On Android Device
Solution 1:
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 :
intUID=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();// recievedlong initialTx = TrafficStats.getTotalTxBytes();// sentlong 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
Post a Comment for "How To Write A Service To Keep Track Of Gprs Data Usage On Android Device"