Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.your.example;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import com.unity3d.player.UnityPlayer;
- public class ServiceMessageReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- // Check if the received intent matches your action
- if ("com.your.example.MESSAGE".equals(intent.getAction())) {
- // Extract the number sent from the service
- int number = intent.getIntExtra(Intent.EXTRA_TEXT, -1);
- Log.d("ServiceMessageReceiver", "Received number: " + number);
- sendMessageToUnity(number);
- }
- }
- private void sendMessageToUnity(int number) {
- // Run on Unity's main thread to send message to a GameObject in Unity
- UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- try {
- // Replace "GameObjectName" with the name of the GameObject in Unity
- // Replace "OnServiceMessageReceived" with the Unity method name
- UnityPlayer.UnitySendMessage("GameObjectName", "OnServiceMessageReceived", String.valueOf(number));
- } catch (Exception e) {
- Log.e("ServiceMessageReceiver", "Error sending message to Unity: ", e);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement