我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
我需要弄清楚如何获取或制作Android应用程序的版本号。我需要在UI中显示内部版本号。
我必须使用AndroidManifest.xml吗?
当前回答
就写这个。
XML代码
<TextView
android:id="@+id/appversion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
初始化textview的变量
TextView appversion;
将textview id设置为变量
appversion = findViewById(R.id.appversion);
这是显示应用程序版本BuildConfig.version_NAME的主要代码,仅将其用作字符串值
textview.setText("v"+BuildConfig.VERSION_NAME);
其他回答
第一:
import android.content.pm.PackageManager.NameNotFoundException;
然后使用这个:
PackageInfo pInfo = null;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
}
catch (NameNotFoundException e) {
e.printStackTrace();
}
String versionName = pInfo.versionName;
package com.sqisland.android.versionview;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewversionName = (TextView) findViewById(R.id.text);
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
textViewversionName.setText(packageInfo.versionName);
}
catch (PackageManager.NameNotFoundException e) {
}
}
}
由于我只需要获取版本代码并检查应用程序是否更新,如果是,我必须启动play商店以获取更新的应用程序。我是这样做的。
public class CheckForUpdate {
public static final String ACTION_APP_VERSION_CHECK = "app-version-check";
public static void launchPlayStoreApp(Context context)
{
// getPackageName() from Context or Activity object
final String appPackageName = context.getPackageName();
try {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appPackageName)));
}
catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" +
appPackageName)));
}
}
public static int getRemoteVersionNumber(Context context)
{
int versionCode = 0;
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
versionCode = pInfo.versionCode;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
然后,我通过创建一个util类,使用共享首选项保存了版本代码。
public class PreferenceUtils {
// This is for version code
private final String APP_VERSION_CODE = "APP_VERSION_CODE";
private SharedPreferences sharedPreferencesAppVersionCode;
private SharedPreferences.Editor editorAppVersionCode;
private static Context mContext;
public PreferenceUtils(Context context)
{
this.mContext = context;
// This is for the app versioncode
sharedPreferencesAppVersionCode = mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
editorAppVersionCode = sharedPreferencesAppVersionCode.edit();
}
public void createAppVersionCode(int versionCode) {
editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
editorAppVersionCode.apply();
}
public int getAppVersionCode()
{
return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // As the default version code is 0
}
}
这是一个干净的解决方案,基于scottyab(由哈维编辑)的解决方案。它显示了如果方法没有提供上下文,如何首先获取上下文。此外,它使用多行而不是每行调用多个方法。这使您在调试应用程序时更容易。
Context context = getApplicationContext(); // or activity.getApplicationContext()
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();
String myVersionName = "not available"; // initialize String
try {
myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
现在您在StringmyVersionName中收到了版本名,您可以将其设置为TextView或任何您喜欢的内容。。
// Set the version name to a TextView
TextView tvVersionName = (TextView) findViewById(R.id.tv_versionName);
tvVersionName.setText(myVersionName);
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String versionName = packageInfo.versionName;
int versionCode = packageInfo.versionCode;
//binding.tvVersionCode.setText("v" + packageInfo.versionName);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}