xml 示例如何使用Unity3D和SMS Manager在Android中发送SMS。 (无需打开SMS Composer)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 示例如何使用Unity3D和SMS Manager在Android中发送SMS。 (无需打开SMS Composer)相关的知识,希望对你有一定的参考价值。

using UnityEngine;

public class SendSMS : MonoBehaviour
{
    AndroidJavaObject currentActivity;

    public void Send(string phone)
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            RunAndroidUiThread();
        }
    }

    void RunAndroidUiThread()
    {
        AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(SendProcess));
    }

    void SendProcess()
    {
        Debug.Log("Running on UI thread");
        AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
        
        // SMS Information
        
        string phone = "08888888888";
        string text = "Hello World. This SMS is sent using Android SMS Manager.";
        string alert;

        try
        {
            // SMS Manager

            AndroidJavaClass SMSManagerClass = new AndroidJavaClass("android.telephony.SmsManager");
            AndroidJavaObject SMSManagerObject = SMSManagerClass.CallStatic<AndroidJavaObject>("getDefault");
            SMSManagerObject.Call("sendTextMessage", phone, null, text, null, null);

            alert = "Message sent successfully.";
        }
        catch (System.Exception e)
        {
            Debug.Log("Error : " + e.StackTrace.ToString());

            alert = "Error has been Occurred. Fail to send message.";
        }

        // Show Toast

        AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
        AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", alert);
        AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject>("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT"));
        toast.Call("show");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Directory : [Project]/Assets/Plugins/Android/AndroidManifest.xml -->
<!-- Configure according to your android application information. -->
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
	android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">
  <supports-screens
      android:smallScreens="true"
      android:normalScreens="true"
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true"/>
  
  <!-- REQUIRED permission -->
  <uses-permission android:name="android.permission.SEND_SMS" />
  <!-- Additional -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.STORAGE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.SMS_RECEIVED" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

  <application
  android:theme="@style/UnityThemeSelector"
  android:icon="@drawable/app_icon"
      android:label="@string/app_name"
      android:debuggable="true">
    <activity android:name="com.unity3d.player.UnityPlayerActivity"
              android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
  </application>
</manifest>

以上是关于xml 示例如何使用Unity3D和SMS Manager在Android中发送SMS。 (无需打开SMS Composer)的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D 从 XML 文件保存和加载数据不起作用

PHP示例SMS代码-消息传递API

html 带有SMS选项的PHP SMTP电子邮件示例

Unity3d 新建xml 读取xml

如何使用 Unity3d 连接到本地解析服务器?

003-unity3d 物理引擎简介以及示例