show code block

2017年7月3日 星期一

Android元件 — 獲取手機識別碼、版本號碼

前言:

手機唯一識別碼
每一台手機上都是獨一無二的。
應用場景可以在Google上看到,當你在A手機綁定了Google帳號,你只要拿其他手機登入你的帳號,Google馬上會判別這不是你之前常用的A手機的識別碼,然後發訊息提醒你『你有登入訊息唷!是否被盜帳號了呢?』

版本號碼
就是要拿取你的VersionName
通常VersionName是給使用者看的,VersionCode是給開發者自己更新版本確認用的。




重點程式碼:




獲取手機識別碼

 //DeviceId
    @SuppressLint("HardwareIds")
    public static String getDeviceId() {
        UUID uuid = null;
        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        try {
            uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return uuid.toString();
    }
  
 
獲取app版本號碼VersionName
   
    //獲取VersionName
    public static String getAppVersion() {
        String version = "";
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }
    //獲取VersionCode
    public static String getAppVersionNumber() {
        String version = "";
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = String.valueOf(info.versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }
 

通常以上方法是複寫在自己的Application內,因為實作方便,就在Activity內做示範。



實作:


activity_main.xml
  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.ellipsize.kaiser.version.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/deivceId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="deviceiD" />

        <TextView
            android:id="@+id/versionCode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="deviceiD" />

        <TextView
            android:id="@+id/versionNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="deviceiD" />


    </LinearLayout>


</RelativeLayout>

 
strings.xml
  
<resources>
    <string name="app_name">Version</string>
    <string name="Deviceid">DeviceId : %s</string>
    <string name="VersionName">VersionName : %s</string>
    <string name="VersionCode">VersionCode : %s</string>

</resources>
 

MainActivity.java
  public class MainActivity extends AppCompatActivity {

    public static Context context;

    private TextView DeviceID, versionName, versionCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        DeviceID = (TextView) findViewById(R.id.deivceId);
        versionCode = (TextView) findViewById(R.id.versionCode);
        versionName = (TextView) findViewById(R.id.versionNumber);

        DeviceID.setText(getString(R.string.Deviceid, getDeviceId()));
        versionName.setText(getString(R.string.VersionName, getAppVersion()));
        versionCode.setText(getString(R.string.VersionCode, getAppVersionName()));

    }

    //DeviceId
    @SuppressLint("HardwareIds")
    public static String getDeviceId() {
        UUID uuid = null;
        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        try {
            uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return uuid.toString();
    }

    public static String getAppVersion() {
        String version = "";
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    public static String getAppVersionName() {
        String version = "";
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = String.valueOf(info.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }
}

 



完成囉!

很簡單就不給Demo了。

沒有留言:

張貼留言

協程(coroutine) - 協程為什麼要學它?

 Coroutine 協程 再強調一次 協程就是由kotlin官方所提供的線程api //Thread Thread { }.start() //Executor val execu...