Android应用获取上次更新日期

时间:2013-05-25 10:37:34

标签: android

是否可以获取应用上次更新的日期? 我想在应用程序中显示它:

  String htmltext = "E-mail: " + Datacontainer.Instance().GetDatabaseHelper().GetUserEmail() +
                    "<br>Authorized: " + is_auth +
                    "<br>Last updated on: " + "?" +
                    "<br>Tips version: " + "228" +
                    "<br>App version: " + "v 1.3.0" +
                    "<br>Your Airline: " + "?" +
                    "<br>Your Nickname: " + "?" +
                    "<br>No of tips submitted: " + "?";

  TextView infotext = (TextView)fragmentView.findViewById(R.id.infotext);
  infotext.setText(Html.fromHtml(htmltext));

2 个答案:

答案 0 :(得分:3)

试试这个:

PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified(); //Epoch Time

答案 1 :(得分:0)

此方法以字符串格式返回上次更新的日期,如12/25/2016 10:38:02

// get last update date
private String getLastUpdateDate() {

    PackageManager packageManager =  getActivity().getPackageManager();
    long updateTimeInMilliseconds; // install time is conveniently provided in milliseconds

    Date updateDate = null;
    String updateDateString = null;

    try {

        ApplicationInfo appInfo = packageManager.getApplicationInfo(getActivity().getPackageName(), 0);
        String appFile = appInfo.sourceDir;
        updateTimeInMilliseconds = new File(appFile).lastModified();
        updateDateString  = MiscUtilities.getDate(updateTimeInMilliseconds, "MM/dd/yyyy hh:mm:ss");

    }
    catch (PackageManager.NameNotFoundException e) {
        // an error occurred, so display the Unix epoch
        updateDate = new Date(0);
        updateDateString = updateDate.toString();
    }

    return updateDateString;
}