安卓原生分享

之前在项目里,产品经理提出要加入安卓和IOS的微信,QQ,微博等的分享功能,要实现分享主要有两种方法,一种是接ShareSDK,一种是原生分享,ShareSDK需要在微信开发者平台上申请,一般需要3到5天才能审核通过,这种方法很耗时间,而我们的项目需要一周上线,很明显没有充足的时间,只能采用第二种分享,调用原生分享,这种方法方便快捷。安卓分享实际上是调用U3d的接口,IOS分享实际上是使用IOS开发,通过调用OC代码来实现的,IOS分享会在下一篇博客里给大家介绍。下面附上我的安卓分享的demo。在点击按钮时调用下面的Share方法即可。

public static void Share()
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            //定义AndroidJavaClass变量
            AndroidJavaClass UnityPlayer;
            AndroidJavaClass Intent;
            AndroidJavaClass Uri;
            AndroidJavaClass Environment;

            //实例化AndroidJavaClass变量
            UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            Intent = new AndroidJavaClass ("android.content.Intent");
            Uri = new AndroidJavaClass ("android.net.Uri");
            Environment = new AndroidJavaClass ("android.os.Environment");

            AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

            AndroidJavaObject jstr_type = new AndroidJavaObject("java.lang.String", "text/plain");
            AndroidJavaObject jstr_content = new AndroidJavaObject("java.lang.String", "https://play.google.com/store/apps/details?id= " + Application.identifier);


            AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", Intent.GetStatic<AndroidJavaObject>("ACTION_SEND"));
            intent.Call<AndroidJavaObject>("setType", jstr_type);
            intent.Call<AndroidJavaObject>("putExtra", Intent.GetStatic<AndroidJavaObject>("EXTRA_TEXT"), jstr_content);

            currentActivity.Call("startActivity", intent);

            #endif

        }


同时可能会有发送邮件的需求,这种也很简单。下面附上代码和效果。

 public static void SendEmail (string emailAddress)
        {
            string subject = MyEscapeURL ("To:" + Application.productName);
            string body = MyEscapeURL ("To the developer team!");
            Application.OpenURL ("mailto:" + emailAddress + "?subject=" + subject + "&body=" + body);

        }

 private static string MyEscapeURL (string url)
        {
            return WWW.EscapeURL (url).Replace ("+", "%20");
        }



阅读更多
个人分类: 分享
下一篇IOS原生分享
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭