Code Snippet - Android App That Displays Data Usage

Posted by Unknown Selasa, 24 April 2012 0 komentar
This is the first in the series of code snippet blog posts!

I'm going to be trying to follow a standard format for all the code snippet blog posts but that may change over time.

Packages Used:


  • android.app.Activity - The activity class is one of the main classes in Android development. An activity represents a single screen with a user interface. 
  • android.app.ActivityManager - Activity Managers interact with the activities in the app.
  • android.app.ActivityManager.RunningAppProcessInfo - Used to retrieve information about a running app.
  • android.net.TrafficStats - Used to retrieve network traffic statistics.
  • android.os.Bundle - Maps String values to Parcelable types.
  • android.os.Process - Contains tools to manage Android OS processes.

Class - AndroidDataOffenders:


The naming for this class is pretty hilarious since all the app does is display how much data each app is using. Therefore, the "Data Offenders" consists of all the apps running on the device. Overall, the app retrieves the phone's traffic statistics and then outputs every app's data usage.

Function - onCreate:


The onCreate function is called when the app is first created (i.e. when it's first ran). 

The function first calls super on onCreate - it's pretty much a standard to perform this function for non-complex apps.

The function then creates an instance of TrafficStats to use it for retrieving the traffic statistics of the phone.

The next two lines seem a bit pointless to me... getMobileRxBytes() retrieves the total amount of bytes received while getTotalRxBytes() retrieves the total amount of bytes sent. The reason why I think it's pointless is that these numbers are never stored in any variable or used in the future. As far as I know, these don't have a dual purpose as initialization functions.


The two lines after simple retrieves the user ID for the device and creates an instance of ActivityManager used to retrieve the running apps later on.


After that, everything is ready to find the data usage. For every running process, it retrieves it's RunningAppProcessInfo. Every process info has a processName as well as a UID. The processName is displayed and the UID of the process is used to get the total bytes used for that particular UID.


After everything is displayed, the app sets the view to the main view for the app which just says "Hello".


Conclusion

In conclusion, this app shows how simple it is to retrieve data usage in the Android system. Diving in to this piece of code allowed me to gain more insight on developing an app that deals with data usage on Android devices.
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Code Snippet - Android App That Displays Data Usage
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidlifehacker4.blogspot.com/2012/04/code-snippet-android-app-that-displays.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android lifehacker.