結(jié)合這段時(shí)間的技術(shù)調(diào)研,做個(gè)總結(jié):
1.針對(duì)5.0以上的安卓系統(tǒng)虎谢,有系統(tǒng)API:可以使用android.app.usage.UsageStatsManager,閑話(huà)不多說(shuō)盟榴,直接po代碼如下:
? ?mUsageStatsManager= (UsageStatsManager).getSystemService(Context.USAGE_STATS_SERVICE);
? ?Calendar cal = Calendar.getInstance();
? ?cal.add(Calendar.YEAR, -1);
? List queryUsageStats = getUsageStatistics(1);
? PackageManager pm = getApplicationContext().getPackageManager();
for(inti =0; i < queryUsageStats.size(); i++) {
? ? ? ? ? ? UsageStats usageStats = queryUsageStats.get(i);
? ? ? ? ? ? ?Class c = usageStats.getClass();
try{
//mLaunchCount 我理解的它就是是app啟動(dòng)次數(shù),這是被@hide修飾的成員變量嘉冒。只能用反射得到它的值曹货。但是有個(gè)現(xiàn)象是,啟動(dòng)次不
//準(zhǔn)讳推,從桌面點(diǎn)進(jìn)去記數(shù)2顶籽,從后臺(tái)進(jìn)入記數(shù)1,時(shí)間間隔為一天银觅,總的統(tǒng)計(jì)次數(shù)也不準(zhǔn)確
? ? ? ? ? ? ? Field field = c.getDeclaredField("mLaunchCount");
? ? ? ? ? ? ? ?field.setAccessible(true);
? ? ? ? ? ? ? ?Field field1 = c.getDeclaredField("mBeginTimeStamp");
? ? ? ? ? ? ? ?field1.setAccessible(true);
? ? ? ? ? ? ? ?Field field2 = c.getDeclaredField("mEndTimeStamp");
? ? ? ? ? ? ? ?field2.setAccessible(true);
? ? ? ? ? ? ? ? ApplicationInfo applicationInfo = pm.getApplicationInfo(usageStats.getPackageName(), ? ? ? ? ? ? ? ? ?PackageManager.GET_META_DATA);
? ? ? ? ? ? ? ?String appName = pm.getApplicationLabel(applicationInfo).toString();
? ? ? ? ? ? ? ?PackageInfo packageInfo = pm.getPackageInfo(usageStats.getPackageName(),0);
//應(yīng)用首次裝時(shí)間
? ? ? ? ? ? ? ? longfirstInstallTime = packageInfo.firstInstallTime;
//格式化成年月日
? ? ? ? ? ? ? ? String firstInstallTime1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", firstInstallTime);
//應(yīng)用最后一次更新時(shí)間
? ? ? ? ? ? ? ?long lastUpdateTime = packageInfo.lastUpdateTime;
? ? ? ? ? ? ? ?String lastUpdateTime1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", lastUpdateTime);
? ? ? ? ? ? ? ? Log.i("應(yīng)用:"+usageStats.getPackageName(),"first install time : "+ firstInstallTime1 +" last update time :"+ lastUpdateTime1);
//這部分的時(shí)間礼饱,我沒(méi)有搞太明白,請(qǐng)自行驗(yàn)證
long firstTimeStamp = usageStats.getFirstTimeStamp();
String firstTimeStamp1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", firstTimeStamp);
long LastTimeStamp = usageStats.getLastTimeStamp();
String LastTimeStamp1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", LastTimeStamp);
long LastTimeUsed = usageStats.getLastTimeUsed();
String LastTimeUsed1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", LastTimeUsed);
long mBeginTimeStamp = field1.getLong(usageStats);
String mBeginTimeStamp1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", mBeginTimeStamp);
long mEndTimeStamp = field2.getLong(usageStats);
String mEndTimeStamp1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", mEndTimeStamp);
//這部分的時(shí)間究驴,我沒(méi)有搞太明白镊绪,請(qǐng)自行驗(yàn)證
? ? ? ? ? ? ? ? Log.i("使用列表...field...","應(yīng)用名:"+ appName +"\n包名:"+ usageStats.getPackageName() +"\nfirstTimeStamp:"+ ? ? ? ? ? ? ? firstTimeStamp1
+"\nLastTimeStamp:"+ LastTimeStamp1 +"\nLastTimeUsed:"+ LastTimeUsed1
+",\n啟動(dòng)次數(shù):"+ field.getInt(usageStats)
+"洒忧,\nmBeginTimeStamp:"+mBeginTimeStamp1+"蝴韭,\nmEndTimeStamp:"+mEndTimeStamp1);
}catch(Exception e) {
? ? ? ? ? ? ? ?e.printStackTrace();
}
}
publicList getUsageStatistics(intintervalType) {// Get the app statistics since one year ago from the current time.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
List queryUsageStats =mUsageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(),
System.currentTimeMillis());
if(queryUsageStats.size() ==0) {
Log.i("使用列表...","The user may not allow the access to apps usage. ");
Toast.makeText(MainActivity.this,
getString(R.string.action_settings),
Toast.LENGTH_LONG).show();
//? ? ? ? ? ? mOpenUsageSettingButton.setVisibility(View.VISIBLE);
//? ? ? ? ? ? mOpenUsageSettingButton.setOnClickListener(new View.OnClickListener() {
//? ? ? ? ? ? ? ? @Override? ? ? ? ? ? ? ? public void onClick(View v) {
//? ? ? ? ? ? ? ? ? ? startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
//? ? ? ? ? ? ? ? }
//? ? ? ? ? ? });
startActivity(newIntent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
}
returnqueryUsageStats;
}
關(guān)于5.0以上系統(tǒng)統(tǒng)計(jì)啟動(dòng)次數(shù),系統(tǒng)提供API不準(zhǔn)熙侍,如果不在第三方應(yīng)用植入自己的sdk,統(tǒng)計(jì)第三方應(yīng)用的啟動(dòng)次數(shù)不靠譜榄鉴,但是要收集安裝時(shí)間是可以的。