Skip to content

Commit

Permalink
增加了一个获取版本名的方法,自动获取,无需每次手动更改代码,并修改版本号至6--1.41
Browse files Browse the repository at this point in the history
  • Loading branch information
imldy committed Jul 2, 2020
1 parent 72f6208 commit adcc85b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.example.toolbox"
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionName "1.4"
versionCode 6
versionName "1.41"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/com/example/toolbox/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.toolbox;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -72,7 +76,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("关于")
.setIcon(R.mipmap.ic_launcher)
.setMessage("版 本:1.2\n开发者:李德银 于小雨 张守华")
.setMessage("版 本:" + getAppVersionName(this) + "\n开发者:李德银 于小雨 张守华")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand All @@ -85,4 +89,25 @@ public void onClick(DialogInterface dialog, int which) {
}
return super.onOptionsItemSelected(item);
}

/**
* 返回当前程序版本名
*/
public static String getAppVersionName(Context context) {
String versionName = "";
int versioncode = 0;
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
versioncode = pi.versionCode;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e);
}
return versionName;
}
}

0 comments on commit adcc85b

Please sign in to comment.