繁体   English   中英

如何使用 react-native-splash-screen 库在 Android (React Native) 初始屏幕上添加应用程序版本

[英]How to add app version on Android (React Native) splash screen using react-native-splash-screen library

我正在使用 react-native-splash-screen 库来显示我们的 react native 应用程序的简单启动画面。 我不太确定如何在同一个初始屏幕上添加应用程序版本。

这是布局文件-launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/background_splash"
      android:orientation="vertical">

    <TextView
        android:id="@+id/appVersion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="V0.0.0"
        android:textColor="#f00"
        android:textSize="25sp" />
</LinearLayout>

这是可绘制的 - background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/splashscreen_bg"/>
    <item
        android:drawable="@mipmap/splash_icon"
        android:gravity="center" />
</layer-list>

MainActivity file where I am fetching the latest app version and setting that on textView but It is throwing an error -> Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference .

MainActivity.java

public class MainActivity extends ReactActivity {
static String currentLocale;
    /**
     * Avoiding the blank screen between splash and app
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);

        SplashScreen.show(this);

        Context context = this.getApplicationContext();
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();

        String appVersion = "not available"; // initialize String

        try {
              appVersion = packageManager.getPackageInfo(packageName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
        }

        TextView appVersionName = (TextView)findViewById(R.id.appVersion);
        try {
            appVersionName.setText(appVersion);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
        }

        MainActivity.currentLocale = getResources().getConfiguration().locale.toString();
    }


}



}

如果您使用的是 Gradle 插件/Android Studio,则版本代码和版本名称在 BuildConfig 中静态可用(您可以检查您的应用级别 build.gradle 文件)

它应该看起来像这样

defaultConfig {
 versionCode 1
 versionName "1.0"
}

因此,您可以使用YOUR_PACKAGE_NAME.BuildConfig简单地获取您的 App versionName 和 versionCode

这里是 go:

import your_package_name_here.BuildConfig;

onCreate方法内部

int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;

谢谢:)

方法

在编译时创建一个字符串资源文件,该文件将用于 TextView 以显示应用程序版本。

步骤 - (1)我们可以使用 gradle 创建字符串资源,所以这里是 gradle 文件中的更改 -

android {
...
    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    ...
    applicationVariants.all { variant ->    
       // This will generate versionName string resource.
       // We going to use same generated resource to show app version on splash screen.
       variant.resValue "string", "versionName", "v" + variant.versionName
    }
    ...
}

这将在编译期间在generated/res中创建一个资源文件generated.xml 有关更多详细信息,请查看 OleGG 的这个答案

步骤 - (2) launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/background_splash"
      android:orientation="vertical">

    <TextView
        android:id="@+id/appVersion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/versionName" />

</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM