Native Plugin to get Users phone number
Hi, I am a bit new to creating native plugins for unity. I wanted to get the users phone number. I came up with the code below but it is not working. Does anyone know what I am doing wrong?
Plugin
package com.weza.mwitotelylib;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
public class AndroidPluginAccess
{
public String ReturnSIMPhoneNumber( Activity curActivity )
{
TelephonyManager telephonyManager = (TelephonyManager)curActivity.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getLine1Number();
}
}
Unity Code
using UnityEngine;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
public class GetAndroidPluginData : MonoBehaviour
{
public string ReturnSIMPhoneNumber()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission("android.permission.READ_PHONE_STATE"))
{
Permission.RequestUserPermission("android.permission.READ_PHONE_STATE");
}
#endif
AndroidJavaClass jcUnityPlayer =
new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject joUnityActivity =
jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject joAndroidPluginAccess =
new AndroidJavaObject("com.weza.mwitotelylib");
return joAndroidPluginAccess.Call<string>("ReturnSIMPhoneNumber", joUnityActivity);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to use App indexing Api's by extending a class with Prime31 's UnityPlayerActivity ? 0 Answers
Unity Android Plugin onActivityResult not called 0 Answers
JAVA_HOME environment references a directory 1 Answer
i want to use dji sdk in unity, i tried to make unity android plugin, but it doesn't work 3 Answers