Unity安卓平台开发,启动app并传参

csharp 复制代码
using UnityEngine;
using System;

public class IntentReceiver : MonoBehaviour
{
    public bool isVR1;
    void Start()
    {
        Debug.LogError("app1111111111111111111111111");
        if (isVR1)
        {
            LaunchAnotherApp("com.HappyMaster.DaKongJianVR2");
        }
        else
        {
            // 检查是否有传递的参数
            if (Application.isMobilePlatform)
            {
                AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

                AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");

                // 检查Intent是否包含额外的数据
                if (intent != null)
                {
                    // 从Intent中获取参数
                    string sn = intent.Call<string>("getStringExtra", "sn");
                    int group = intent.Call<int>("getIntExtra", "group", 0);
                    string app = intent.Call<string>("getStringExtra", "app");
                    string user = intent.Call<string>("getStringExtra", "user");
                    int human = intent.Call<int>("getIntExtra", "human", 0);
                    int times = intent.Call<int>("getIntExtra", "times", 0);

                    // 在这里处理接收到的参数
                    Debug.Log($"Received parameter: \n{sn}\n{group}\n{app}\n{user}\n{human}\n{times}");
                }
            }
        }
    }


    void LaunchAnotherApp(string packageName)
    {
        try
        {
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
            AndroidJavaObject launchIntent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", packageName);
            if (launchIntent == null)
            {
                Debug.Log("无法获取 launch intent");
                return;
            }

            // 添加需要的 extras 和 flags
            launchIntent.Call<AndroidJavaObject>("putExtra", "sn", "PicoSN");
            launchIntent.Call<AndroidJavaObject>("putExtra", "group", 99);
            launchIntent.Call<AndroidJavaObject>("putExtra", "app", "一刀九九");
            launchIntent.Call<AndroidJavaObject>("putExtra", "user", "教主");
            launchIntent.Call<AndroidJavaObject>("putExtra", "human", 0);
            launchIntent.Call<AndroidJavaObject>("putExtra", "times", 2025);

            currentActivity.Call("startActivity", launchIntent);
        }
        catch (Exception e)
        {
            Debug.LogError($"启动app失败: {e.Message}\n{e.StackTrace}");
        }
    }

}

AndroidManifest 需要添加权限

相关推荐
jtymyxmz1 小时前
《Unity Shader》10.1.3 反射
unity·游戏引擎
摆烂积极分子2 小时前
安卓开发学习-安卓版本
android·学习
n***26563 小时前
MySQL JSON数据类型全解析(JSON datatype and functions)
android·mysql·json
t***82113 小时前
mysql的主从配置
android·mysql·adb
YF02115 小时前
Frida如何稳定连接PC端跟Android手机端
android·mac·xposed
O***P5716 小时前
【MySQL】MySQL内置函数--日期函数字符串函数数学函数其他相关函数
android·mysql·adb
z***43846 小时前
MySQL-mysql zip安装包配置教程
android·mysql·adb
无心水7 小时前
【Python实战进阶】7、Python条件与循环实战详解:从基础语法到高级技巧
android·java·python·python列表推导式·python条件语句·python循环语句·python实战案例
g***78918 小时前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
android·前端·后端
Bervin1213814 小时前
Flutter Android环境的搭建
android·flutter