Unity+Android Get mobile phone number

First in Unity:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AndroidTest : MonoBehaviour
{

public Text _text;

public void OnClickBtn()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        _text.text = jo.Call<string>("U3D_GetValue");
    }
}

Hang the script on a GameObject in a scene, specify the UI Text and button method binding, and export the Android project package:

Open with Android Studio:

Android:

import android.content.Context;
import android.telephony.TelephonyManager;
 //unity调用android的方法返回一个字符串
    public String U3D_GetValue(){
        TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
        String te1  = tm.getLine1Number();//获取本机号码
        return te1;
    }

Finally, package the APK in Android Studio and run it on a real machine for process testing. Click the button to get the phone number and display it.

Of course, you can also write the code to obtain the mobile phone number in Android first, export the jar package, import it into Unity for use, the path is Plugin/Android/xxxx.jar, and also need a copy of xml, and then write the code in Unity to call the Jar. The same effect is achieved with hair packages.

Guess you like

Origin blog.csdn.net/hemiaoyuan1989/article/details/105765800