阅读团结应用推出Android意图额外的数据 [英] Read Android intent extra data on Unity app launch

查看:243
本文介绍了阅读团结应用推出Android意图额外的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发起使用自定义的隐含意图另一个Android应用程序的Unity应用。这是工作正常,但我无法弄清楚如何阅读团结的意图额外的数据?

I am launching an Unity application from another Android application using a custom implicit intent. This is working fine, but I cannot figure out how to read the intent extra data in Unity?

ANDROID意图启动Unity APP

ANDROID INTENT TO LAUNCH UNITY APP

i=new Intent();
i.setAction("com.company.unityapp.MyMethod");
i.putExtra("KEY","This is the message string");
startActivity(i);



UNITY APP的Andr​​oidManifest.xml

UNITY APP AndroidManifest.xml

<intent-filter>
     <action android:name="com.company.unityapp.MyMethod" />
     <category android:name="android.intent.category.DEFAULT" />
</intent-filter>



我在我的场景附加脚本中的游戏物体。里面的启动方法我有这样的代码尝试读取与意图

I have a GameObject in my scene with a script attached. Inside the start method I have this code to try and read the extra data that was passed along with the intent

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", "arguments");
}

这不工作和争论总是空空的。任何帮助,将不胜感激。

This is not working and arguments is always empty. Any help would be appreciated.

推荐答案

我花了相当长的一段时间,想出解决办法。在网上找到所有的解决方案只是部分完成。下面是使用自定义的隐含意图,以及如何访问与内部团结的意图发送额外的数据从另一个Android应用推出一个统一应用程序的完整的解决方案。

It took me quite some time to figure this out. All solutions found online were only partly complete. Below is the full solution for launching a Unity application from another android application using a custom implicit intent and also how to access the extra data sent with the intent inside Unity.

要。做到这一点,你需要创建一个Android的插件,将被统一用于访问的意图额外的数据

To accomplish this you need to create a Android plugin that will be used by Unity to access the intent extra data.

Android插件:

ANDROID PLUGIN:

您需要从统一安装文件夹复制classes.jar到Android插件文件夹/lib/classes.jar

You need to copy the classes.jar from Unity installation folder to the android plugin folder /lib/classes.jar

public class MainActivity extends UnityPlayerActivity {

  @Override
  protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      handleNewIntent(intent);
  }

  private void handleNewIntent(Intent intent){
      String text = intent.getStringExtra("KEY");
      UnityPlayer.UnitySendMessage("AccessManager","OnAccessToken", text);
  }
}



的Andr​​oidManifest.xml

AndroidManifest.xml

这里重要的是所用的包名:com.company.plugin

Important here is the package name used: com.company.plugin

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin">
    <application
        android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>



摇篮构建文件:

Gradle build file:

添加下面的应用程序构建的gradle文件能够创建一个.jar与统一

Add the following to the app gradle build file to be able to create a .jar to be used with Unity

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
        }
    }       
...
...

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile files('libs/classes.jar')
}

//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'release/AndroidPlugin.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    ///Rename the jar
    rename('classes.jar', 'AndroidPlugin.jar')
}

exportJar.dependsOn(deleteOldJar, build)

复制创建AndroidPlugin.jar到Unity资产/插件/ Android版

Copy the created AndroidPlugin.jar to Unity Assets/Plugins/Android

UNITY APP:

UNITY APP:

将PlayerSettings捆绑标识符作为设置在Android插件一样的 - com.company.plugin

Set the bundle identifier in PlayerSettings to be the same as set in the Android Plugin - com.company.plugin

创建自定义AndroidManifest在资产/插件/ Android的.xml文件

Create custom AndroidManifest.xml file in Assets/Plugins/Android

这里重要的是使用相同的包名作为插件使用。
还要注意的意图名称:com.company.plugin.do

Important here is to use the same package name as used in the plugin. Also note the intent name: com.company.plugin.do

的Andr​​oidManifest.xml

AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.plugin"
      android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name"
          android:launchMode="singleTask" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="sensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.company.plugin.do" />
                <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>
</manifest>



创建一个名为AccessManager一个统一的脚本,并附加脚本在场景中的游戏对象。 OnAccessToken是将接收来自Android插件发送的消息,将包含来自意图发送的额外数据的方法。

Create a unity script named AccessManager and attach the script to a game object in the scene. OnAccessToken is the method that will receive the message sent from the android plugin and will contain the extra data sent from the intent.

public class accessManager : MonoBehaviour {

    public void OnAccessToken(string accessToken)
    {
        Debug.Log("Message Received!!!! :" + accessToken);
    }
}






Android应用:


ANDROID APP:

创建一个标准的Andr​​oid应用程序,将推出Unity应用程序发送的意图额外的数据

Create a standard Android application that will launch the Unity Application and send the intent extra data

public void LaunchUnityApp(){
    Intent i=new Intent();
    i.setAction("com.company.plugin.do");
    i.setType("text/plain");
    i.putExtra("KEY","This is the text message sent from Android");
    startActivity(i);
}

这篇关于阅读团结应用推出Android意图额外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆