Unity 跳转Instagram App
一、ios直接跳转对应网址Application.OpenURL(url)
二、android
1、判断是否有应用
using (var jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (var currentActivity = jc.GetStatic<AndroidJavaObject>(&#34;currentActivity&#34;))
{
using (var packageManager = currentActivity.Call<AndroidJavaObject>(&#34;getPackageManager&#34;))
{
using (var pluginClass = new AndroidJavaClass(&#34;android.content.pm.PackageManager&#34;))
{
int flag = pluginClass.GetStatic<int>(&#34;GET_META_DATA&#34;);
var packages =packageManager.Call<AndroidJavaObject>(&#34;getInstalledApplications&#34;, flag);
int count = packages.Call<int>(&#34;size&#34;);
for (int i = 0; i < count; i++)
{
var pkg = packages.Call<AndroidJavaObject>(&#34;get&#34;, i);
var pkgName = pkg.Get<string>(&#34;packageName&#34;);
if (pkgName == packageName)
{
Debug.LogWarning(&#34;ThirdAppUtil IsPackageInstalled&#34; + packageName);
return true;
}
}
}
}
}
} 2、有应用后打开,packageName=&#34;com.instagram.android&#34;
using (var jc = new AndroidJavaClass(&#34;com.unity3d.player.UnityPlayer&#34;))
{
using (var currentActivity = jc.GetStatic<AndroidJavaObject>(&#34;currentActivity&#34;))
{
using (var packageManager = currentActivity.Call<AndroidJavaObject>(&#34;getPackageManager&#34;))
{
using (var intent = packageManager.Call<AndroidJavaObject>(&#34;getLaunchIntentForPackage&#34;, packageName))
{
if (null != intent)
{
AndroidJavaClass intentClass = new AndroidJavaClass(&#34;android.content.Intent&#34;);
AndroidJavaObject intentObject = new AndroidJavaObject(&#34;android.content.Intent&#34;);
intentObject.Call<AndroidJavaObject>(&#34;setAction&#34;,
intentClass.GetStatic<string>(&#34;ACTION_VIEW&#34;));
AndroidJavaClass uriClass = new AndroidJavaClass(&#34;android.net.Uri&#34;); // 对应的安卓调用函数是Uri.parse()
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>(&#34;parse&#34;,urlScheme);
intentObject.Call<AndroidJavaObject>(&#34;setData&#34;, uriObject);
intentObject.Call<AndroidJavaObject>(&#34;setPackage&#34;,packageName); // 记得要set package他才知道在哪里运行url sheme,否则url会从默认的浏览器里打开
currentActivity.Call(&#34;startActivity&#34;, intentObject);
}
}
}
}
}
urlScheme获取,参考Instagram
3、Instagram安装后未运行,跳转失败处理。manifest内添加包名
<queries>
<!-- 声明将要查询的包名 -->
<package android:name=&#34;com.instagram.android&#34; />
</queries>
页:
[1]