ANDROID sendBroadcast到UNITY BroadcastReceiver无法正常工作

时间:2014-09-11 12:00:20

标签: java android

我正在开发一个Android项目,我偶然发现了一个小问题。我从统一教程页面制作了滚球游戏,现在我想从Android应用程序发送控件。这是至关重要的,我不希望游戏拦截控件本身。我正在使用本教程:

http://jeanmeyblum.weebly.com/scripts--tutorials/communication-between-an-android-app-and-unity

我在android应用程序中提供了一个服务,我认为这很好。如果它在调试器中运行,我测试了它,它就是。然后我根据指令制作接收器,与插件部分相同。当我运行游戏(在Android应用程序中的一个活动中)时,接收器几乎正常工作,他获取我在静态变量中设置的“文本”值,但只获得开头声明中的值(所以我认为插件部分工作正常)。 问题是没有调用onReceive()方法,因此“text”值不会变为“-1”,这会使球向相反方向移动(球总是沿+1方向移动)。 / p>

以下是代码:

public class MyReceiver extends BroadcastReceiver {

    private static MyReceiver instance;
    // text that will be read by Unity
    public static String text = "1";
    // Triggered when an Intent is catched
    @Override
    public void onReceive(Context context, Intent intent) {
        // We get the data the Intent has
        String sentIntent = intent.getStringExtra(Intent.EXTRA_TEXT);
        //if (sentIntent != null) {
            text = "-1";
        //}
    }
    public static void createInstance()
    {
        if(instance ==  null)
        {
            instance = new MyReceiver();
        }   
    }
}

服务:

public class MyService extends Service {

    private final Handler handler = new Handler();
    private int numIntent = 0;

    // It's the code we want our Handler to execute to send data
    private Runnable sendData = new Runnable() {
        // the specific method which will be executed by the handler
        public void run() {
            numIntent++;

            // sendIntent is the object that will be broadcast outside our app
            Intent sendIntent = new Intent();

            // We add flags for example to work from background         
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION|Intent.FLAG_FROM_BACKGROUND|Intent.FLAG_INCLUDE_STOPPED_PACKAGES  );

            // SetAction uses a string which is an important name as it identifies the sender of the intent and that we will give to the receiver to know what to listen.
            // By convention, it's suggested to use the current package name
            sendIntent.setAction("com.example.package");

            // Here we fill the Intent with our data, here just a string with an incremented number in it.
            sendIntent.putExtra(Intent.EXTRA_TEXT, "1000");
            // And here it goes ! our message is send to any other app that want to listen to it.
            sendBroadcast(sendIntent);

            // In our case we run this method each second with postDelayed
            handler.removeCallbacks(this);
            handler.postDelayed(this, 1000);
        }
    };

    // When service is started
    public void onStart(Intent intent, int startid) {
        // We first start the Handler
        handler.removeCallbacks(sendData);
        handler.postDelayed(sendData, 1000);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

我还编辑了清单:

<service
        android:name="com.example.MyService"
        android:enabled="true" >
        </service>

和接收者:

<receiver android:name="com.example.receiver.MyReceiver">
            <intent-filter>
                <action android:name="com.example.package">
                </action>
            </intent-filter>
        </receiver>

我可以在手机设置中看到MyService,游戏获得声明的“文本”值。问题是我很薄的onReceive方法,甚至从未调用过。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:3)

我离开这个项目一段时间了,昨天回来了。我做了。问题是UNITY接收器必须也在ANDROID清单中注册,事实上我认为它必须在两个清单中注册。我希望有一天能帮到某人:D