APP statistics using the Android system

Source: Internet
Author: User

I was working on a small program a few days ago. It was about how Android counts the number of times the app was used and how long it took. Later, I checked some relevant information on the Internet and finally solved the problem.

It is actually very simple. This is a self-contained function on the mobile phone. You do not need to press any combination of buttons at the same time. Instead, you can dial on the dialing interface. Click "Dialer" on the main screen of the system to enter the dialing interface. Enter "* #* #4636 # *" (without quotation marks) to enter the project testing mode. The Testing interface is displayed, which includes four options: Phone information, Battery information, Battery history, and Usage statistics. For example, to view the historical running time of a program, click the last Usage statistics command to list the historical running time of each program.

 

 

LogCat prints the following information:

For example, we can see the Activity package name: com. android. settings Class Name: UsageStates

In this way, we can write a small program to call the program.

 

The Code is as follows:

Main. xml

 

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "vertical" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> <Button <br/> android: id = "@ + id/button" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "Application Usage Statistics" <br/> </p> </LinearLayout> <br/> 

 

ActivityMain. java

Public class ActivityMain extends Activity {<br/> private Button button; <br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main); </p> <p> button = (Button) findViewById (R. id. button); </p> <p> button. setOnClickListener (new Button. onClickListener () {<br/> @ Override <br/> public void onClick (View v) {<br/> Intent intent = new Intent (Intent. ACTION_MAIN); <br/> intent. setComponent (new ComponentName ("com. android. settings "," com. android. settings. usageStats "); <br/> // intent. setClassName ("com. android. settings "," UsageStats "); <br/> startActivity (intent); <br/>}</p> <p> }); </p> <p >}< br/>} 

Program running result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.