【Android】如何统计App启动次数,首次安装时间
结合这段时间的技术调研,做个总结:
1.针对5.0以上的安卓系统,有系统API:可以使用android.app.usage.UsageStatsManager,闲话不多说,直接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启动次数,这是被@hide修饰的成员变量。只能用反射得到它的值。但是有个现象是,启动次不
//准,从桌面点进去记数2,从后台进入记数1,时间间隔为一天,总的统计次数也不准确
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);
//应用首次装时间
longfirstInstallTime = packageInfo.firstInstallTime;
//格式化成年月日
String firstInstallTime1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", firstInstallTime);
//应用最后一次更新时间
long lastUpdateTime = packageInfo.lastUpdateTime;
String lastUpdateTime1 = transferLongToDate("yyyy-MM-dd HH:mm:ss", lastUpdateTime);
Log.i("应用:"+usageStats.getPackageName(),"first install time : "+ firstInstallTime1 +" last update time :"+ lastUpdateTime1);
//这部分的时间,我没有搞太明白,请自行验证
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);
//这部分的时间,我没有搞太明白,请自行验证
Log.i("使用列表...field...","应用名:"+ appName +"\n包名:"+ usageStats.getPackageName() +"\nfirstTimeStamp:"+ firstTimeStamp1
+"\nLastTimeStamp:"+ LastTimeStamp1 +"\nLastTimeUsed:"+ LastTimeUsed1
+",\n启动次数:"+ 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;
}
关于5.0以上系统统计启动次数,系统提供API不准,如果不在第三方应用植入自己的sdk,统计第三方应用的启动次数不靠谱,但是要收集安装时间是可以的。