代码改变世界

Unity发送短信

2016-03-06 19:36  wuzhang  阅读(2165)  评论(2编辑  收藏  举报

  闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码!

1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧!

 

 

 

2,界面极其简单,直接写发送函数。

先创建一个AndroidJavaClass 对象,它用来调用jar包里的函数。

 ajc = new AndroidJavaClass("com.qyxls.sms.SMSActivity");

3,全部代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class SendMessage : MonoBehaviour
{
    public InputField number;
    public InputField content;
    public Button btn_send;
    public Text statue;

    private AndroidJavaClass ajc;

    // Use this for initialization
    void Start()
    {
        ajc = new AndroidJavaClass("com.qyxls.sms.SMSActivity");
        btn_send.onClick.AddListener(delegate()
        {
            this.SendMsg(btn_send.gameObject);
        });
    }

    void SendMsg(GameObject go)
    {
        if (string.IsNullOrEmpty(number.text) || string.IsNullOrEmpty(content.text))
        {
            statue.text = "信息发送失败!";
            return;
        }
        ajc.CallStatic("SMSSend", new string[] { number.text, content.text });
        statue.text = "信息已发送!";
        StartCoroutine(ChangeStatue());
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))
            Application.Quit();
    }

    private IEnumerator ChangeStatue()
    {
        yield return new WaitForSeconds(1f);
        statue.text = "";
        number.text = "";
        content.text = "";
    }

}

4,修改图标,简单粗暴直接修改AndroidManifest文件。

5,真机测试。

测试结果:问候一下10086吧!

静待10s,好了。

工程代码:git@github.com:wuzhangwuzhang/UnitySendMsg.git