android app get the version number of the software realize Notes

Before apk package according to their needs, and the version, the version name set. android studio setting is set to build.gradle file.

 versionCode 1
 versionName "1.0"

versionCode: a local version of the app and the app provides the background were compared for the update function to achieve.

versionName: version information for presentation to the customer to see.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/vbackground"
    tools:context=".versionCodeActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:padding="0dp">


            <ImageButton
                android:id="@+id/back_btn"
                android:layout_width="?attr/actionBarSize"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentLeft="true"
                android:background="@drawable/tab_menu_bg"
                android:src="?attr/homeAsUpIndicator" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="关于"
                android:textSize="19sp" />

        </RelativeLayout>

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="100dp"
            app:srcCompat="@mipmap/logo" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="140dp"
                android:text="@string/version"
                android:textColor="@color/bg_white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/version"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1.1.1"
                android:textColor="@color/bg_white"
                android:textSize="18sp" />

        </LinearLayout>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:background="@color/bg_white"
            android:text="评价我们"
            android:textColor="@color/blue"
            android:textSize="18sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Agreement"
                android:textColor="@color/bg_white" />

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Privacy"
                android:textColor="@color/bg_white" />
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.instructorhelpdome.Dao;

import android.content.Context;
public class APKVersionCodeUtils {
    public static int getVersionCodeUtil(Context context){

        int versionCode = 0;
        try {
            versionCode = context.getPackageManager()//拿到package管理者
                    .getPackageInfo(context.getPackageName(),0).versionCode;

        }catch (Exception e){
            e.printStackTrace();
        }
        return versionCode;//返回versionCode
    }
    
}
package com.example.instructorhelpdome;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.instructorhelpdome.Dao.APKVersionCodeUtils;

import androidx.appcompat.app.AppCompatActivity;

public class versionCodeActivity extends AppCompatActivity implements View.OnClickListener {

    TextView version ;
    Handler  mTimeHandler;
    private ImageButton back_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version_code);

        version =(TextView)findViewById(R.id.version);
        String versionCode = APKVersionCodeUtils.getVersionCodeUtil(this) + "";

        initView();

        //在类里声明一个Handler
        mTimeHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {

                if (msg.what == 0) {

                    version.setText(versionCode);
                    sendEmptyMessageDelayed(0, 1000);
                }
            }


        };

        //在你的onCreate的类似的方法里面启动这个Handler就可以了:
        mTimeHandler.sendEmptyMessageDelayed(0, 1000);

    }

    private void initView() {
        back_btn=(ImageButton)findViewById(R.id.back_btn);
        back_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }

    }
}

 

Published 71 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39131246/article/details/100147195