前幾天在做一個小程式,是關於Android如何來統計應用使用的次數以及使用了多長時間。後來在網上查了一些相關資料,終於解決了。
其實很簡單。這是手機中內建的功能,不用同步選取什麼組合按鍵,而是在撥號介面進行撥號即可。點擊位於系統主屏的Dialer進入撥號介面,輸入“*#*#4636#*#*”(不加引號)即可進入工程測試模式。這時可以看到出現了Testing的介面,包含了Phone information、Battery information、Battery history、Usage statistics四個選項,如,如果需要查看程式的曆史已耗用時間,直接點擊最後一項Usage statistics系統會列出一個列表資訊就是各個程式的曆史已耗用時間 這時LogCat中會列印以下資訊: 因些我們可以看到Activity的 包名:com.android.settings 類名:UsageStates 這樣我們就可以寫一小程式來調用該程式了。 具體代碼如下: 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 /> ><br /> <Button<br /> android:id="@+id/button"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="應用使用統計"<br /> /></p><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 />} 程式運行結果: |