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 需要添加权限

相关推荐
AFinalStone7 分钟前
Android 7系统无障碍服务(五)AccessibilityNodeInfo 的构建与查询
android·无障碍服务
今天要早睡_42 分钟前
C++ 核心语法速过:命名空间、引用、函数重载与 nullptr 深度解析
android·java·c++
啦啦啦~~~2221 小时前
PC端+安卓端阅读器推荐!开源本地小说阅读器软件,
android·论文阅读·windows·开源软件·福昕阅读器
我命由我123452 小时前
Android Jetpack Compose 开发问题:Unresolved reference ‘Icons‘.
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
爱笑鱼2 小时前
Android 系统启动机制(六):Zygote 创建 App 为什么走 Socket?哪些是源码事实,哪些是架构解释?
android·linux
AI2中文网2 小时前
安卓模拟器启动卡在进度条?两步解决硬件加速冲突
android
IT毕设实战小研2 小时前
基于大数据的DAX40成分股金融新闻情感趋势可视化分析
android·大数据·python·考研·金融·课程设计
Android-Flutter3 小时前
flutter async/await 详解
android·flutter
步行cgn3 小时前
Spring Boot 中的 YAML 完全指南
android·spring boot·firefox
事圆则缓4 小时前
Java 常见数据结构与 Android 使用场景
android·java·数据结构