首页 > 解决方案 > 无法在 Unity 上读取 android 意图数据

问题描述

我知道这似乎已经回答了,但它不适用于我,我需要知道是否缺少任何东西。我花了一些时间,但以这种方式读取意图额外数据没有成功。所以我需要知道我是否遗漏了什么。

在没有成功之后,我使用了一种简单的方法

从android插件我调用它来启动统一应用程序:

Intent intent1 = new Intent();

intent1.setAction("com.Company.Alarm.UnityPlayerActivity");

intent1.setFlags(FLAG_ACTIVITY_NEW_TASK);

//intent1.setType("text/plain");

intent1.putExtra("KEY","This is the text message sent from Android");

context.startActivity(intent1);

在 Unity 上,我使用它来获取数据:

string arguments = "";

    AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
    bool hasExtra = intent.Call<bool>("hasExtra", "arguments");

    if (hasExtra)
    {
        AndroidJavaObject extras = intent.Call<AndroidJavaObject>("getExtras");
        arguments = extras.Call<string>("getString", "KEY");
    }
    text.text = arguments;

    Debug.Log("Message Received!!!! :" + arguments);

但是在应用程序启动时,我没有任何价值。请任何人都可以说这里缺少什么或如何解决它?

编辑:

    //manifest
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Company.Alarm" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner">
    <activity android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask"
        android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
        android:hardwareAccelerated="false" android:name="com.Company.x.UnityPlayerActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        <action android:name="com.Company.x.Unity"/>
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <meta-data android:name="unity.build-id" android:value="0e614110-ed04-4c63-b8d5-f8885587d24c" />
    <meta-data android:name="unity.splash-mode" android:value="0" />
    <meta-data android:name="unity.splash-enable" android:value="True" />
  </application>
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
  <uses-feature
      android:name="android.software.leanback"
      android:required="false" />
</manifest>

标签: androidunity3d

解决方案


推荐阅读