古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述在Android上获取应用程序安装日期相关的知识,希望能为你提供帮助。
是否有办法在android设备上找到“安装应用程序的日期”。
广泛搜索,但无法找到相关答案。
通过PackageManager
documentation / Code无法找到有关安装应用程序的日期的任何信息。
答案或者这个(API等级9以上!):
long installed = context
.getPackageManager()
.getPackageInfo(context.getPackag??eName(), 0)
.firstInstallTime
;
另一答案使用此代码:
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified();
另一答案尝试其中之一
/**
* The time at which the app was first installed. Units are as per currentTimeMillis().
* @param context
* @return
*/
public static long getAppFirstInstallTime(Context context){
PackageInfo packageInfo;
try {
if(Build.VERSION.SDK_INT>
8/*Build.VERSION_CODES.FROYO*/ ){
packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return packageInfo.firstInstallTime;
}else{
//firstinstalltime unsupported return last update time not first install time
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
String sAppFile = appInfo.sourceDir;
return new File(sAppFile).lastModified();
}
} catch (NameNotFoundException e) {
//should never happen
return 0;
}
}
另一答案 First Time It Was Installed
activity.getPackageManager().getPackageInfo( activity.getPackageName(), 0 ).firstInstallTime;
Last Time It Was Updated
activity.getPackageManager().getPackageInfo( activity.getPackageName(), 0 ).lastUpdateTime;
另一答案此方法以字符串格式返回安装日期,如
12/25/2016 10:38:02
:private String getInstallDate() {
// get app installation datePackageManager packageManager =getActivity().getPackageManager();
long installTimeInMilliseconds;
// install time is conveniently provided in millisecondsDate installDate = null;
String installDateString = null;
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getActivity().getPackageName(), 0);
installTimeInMilliseconds = packageInfo.firstInstallTime;
installDateString= MiscUtilities.getDate(installTimeInMilliseconds, "MM/dd/yyyy hh:mm:ss");
}
catch (PackageManager.NameNotFoundException e) {
// an error occurred, so display the Unix epoch
installDate = new Date(0);
installDateString = installDate.toString();
}return installDateString;
}
【在Android上获取应用程序安装日期】MiscUtilities
/**
* Return date in specified format.
*
* @param milliSeconds Date in milliseconds
* @param dateFormatDate format
* @return String representing date in specified format
* <
p>
* Date myDate = MiscUtilities.getDate(82233213123L, "dd/MM/yyyy hh:mm:ss.SSS");
*/
public static String getDate(long milliSeconds, String dateFormat) {
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
推荐阅读
- android.permission.CALL_PHONE
- 在Windows上获取Android的SHA键
- PhoneGap - 后退按钮上的android退出
- Ionic Build Android - build.gradle文件不断添加重复的编译行并导致构建失败
- 将电子应用转移到cordova app
- 将OneSignal插件添加到Ionic App后生成APK时出错
- Ionic app,错误(..main.ts模块构建失败:错误:ENOENT)
- 从Android访问Windows WebService获取IP地址
- 本文教你u盘不显示怎样办