[英]How do I send data from a class in one package to a class in another package?
I have developed an Android app which opens up a Unity app which will then return to the Android app.我开发了一个 Android 应用程序,它打开一个 Unity 应用程序,然后返回到 Android 应用程序。 I have created a plugin Java class MyPlugin which acts as a bridge between Android and Unity.
我创建了一个插件 Java 类 MyPlugin,它充当 Android 和 Unity 之间的桥梁。 Unity calls this method and passes in a float as a parameter.
Unity 调用此方法并传入一个浮点数作为参数。 The plugin MyPlugin is in the package com.example.unity and is in the path unityLibrary/java.
插件 MyPlugin 位于包 com.example.unity 中,并且位于路径 unityLibrary/java 中。
How do I access this float from classes in my main package in app/java?如何从 app/java 主包中的类访问这个浮点数? Or send this data from MyPlugin to the other classes?
或者将这些数据从 MyPlugin 发送到其他类?
My project structure looks like this:我的项目结构如下所示:
-app
-java
-com.company.package1
-Android class files
-unityLibrary
-java
-com
-example.unity
-MyPlugin.java
I have tried using an intent but aren't sure exactly what to do.我曾尝试使用意图,但不确定该怎么做。 All the examples I have seen deal with everything being in the same package.
我见过的所有示例都处理同一个包中的所有内容。 This is what I tried.
这是我尝试过的。 printText() is a method in MyPlugin which is called from Unity:
printText() 是 MyPlugin 中的一个方法,它是从 Unity 调用的:
public void printText(float num)
{
surfaceArea = num;
System.out.println("Text from Unity: "+num);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, ""+num);
intent.setType("text/plain");
mainActivity.startActivity(intent);
}
Update 16/07/2021: Following Rolland Costomarob's advice, I am now using a Broadcast sender and receiver. 2021 年 7 月 16 日更新:按照 Rolland Costomarob 的建议,我现在正在使用广播发送器和接收器。 My plugin now looks like this:
我的插件现在看起来像这样:
package com.example.unity;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
public class MyPlugin {
private static final MyPlugin ourInstance = new MyPlugin();
private static final String LOGTAG = "AltGras";
public static MyPlugin getInstance() { return ourInstance; }
public static BroadcastSender mainActivity;
public float surfaceArea ;
private MyPlugin() {
mainActivity = new BroadcastSender();
Log.i(LOGTAG, "Created MyPlugin");
}
public void printText(float num)
{
surfaceArea = num;
System.out.println("printText: Text from Unity: "+num);
mainActivity.SendBroadcast(num);
}
}
BroadcastSender.java: BroadcastSender.java:
package com.example.unity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BroadcastSender extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
}
public void SendBroadcast(float surfaceArea){
System.out.println("In MainActivityUnity.SendBroadcast");
Intent intent = new Intent();
if (intent == null)
{
System.out.println("SendBroadcast: intent is null");
}
if (this == null)
{
System.out.println("SendBroadcast: this is null");
}
intent.setAction("com.alternativegrass.alternativegrassapp.CUSTOM_SENDDATA_INTENT");
System.out.println("SendBroadcast: intent = "+intent);
intent.putExtra("com.alternativegrass.alternativegrassapp.EXTRA_SURFACEAREA", ""+surfaceArea);
sendBroadcast(intent);
}
}
I get the following error:我收到以下错误:
AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
The line it is complaining about is: sendBroadcast(intent);它抱怨的那一行是:sendBroadcast(intent);
please try with broadcast and receiver.请尝试使用广播和接收器。 intent can't use for the different app.
意图不能用于不同的应用程序。 so you need to send broadcast from one app, receive data with filter.
所以你需要从一个应用程序发送广播,用过滤器接收数据。
https://developer.android.com/guide/components/broadcasts https://www.tutorialspoint.com/android/android_broadcast_receivers.htm https://developer.android.com/guide/components/broadcasts https://www.tutorialspoint.com/android/android_broadcast_receivers.htm
for example例如
in your manifest file在您的清单文件中
<receiver android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
and define receiver并定义接收器
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
String log = sb.toString();
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
and register receiver并注册接收者
BroadcastReceiver br = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
send broadcast like this像这样发送广播
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("data", "Nothing to see here, move along.");
sendBroadcast(intent);
you will receive message.你会收到消息。
this is sending part (first apk)这是发送部分(第一个 apk)
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
SendBroadcast();
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.sendEmptyMessage(0);
}
public void SendBroadcast(){
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("Value",109.3f);
intent.putExtra("Message","Working well");
Log.e("Broadcast","Send broadcast");
sendBroadcast(intent);
handler.sendEmptyMessageDelayed(0,10000);
}
this broadcast data every 10 sec此广播数据每 10 秒一次
and this is receive part (second apk)这是接收部分(第二个 apk)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver br = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.broadcast.MY_NOTIFICATION");
this.registerReceiver(br, filter);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("Message: " + intent.getStringExtra("Message") + "\n");
sb.append("Value: "+ intent.getFloatExtra("Value",0.0f));
String log = sb.toString();
Log.e("Broadcast ","Received");
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
and this is result log这是结果日志
2021-07-16 01:18:40.488 2337-2337/com.example.broadcastreceiver E/Broadcast: Received 2021-07-16 01:18:40.488 2337-2337/com.example.broadcastreceiver D/MyBroadcastReceiver: Action: com.example.broadcast.MY_NOTIFICATION Message: Working wellValue: 109.3 2021-07-16 01:18:40.488 2337-2337/com.example.broadcastreceiver E/Broadcast: 收到 2021-07-16 01:18:40.488 2337-2337/com.example.broadcastreceiver:ReceiveMyBroadcastreceiver .example.broadcast.MY_NOTIFICATION 消息:运行良好值:109.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.