Introduction
I'll assume you already made some android app. For simplification, I won't speak about in default android app creation but just tell you what you need to know to get this done.
How to send information between two Android app
Sending App
Let's have a look on how to that:
- First of all, create a new class inside your sending app, deriving from android.app.Service. This class is the service that will send an Intent to the others apps.
- Now you have to make your service running inside your app. To do so, you can start the service in any Activity simply by using startService(new Intent(this, MyService.class)); where MyService is the name I called my new service derived class.
- When you start it, a new instance of the service is created and directly call its onStart method.
Here's an example of a really simple service, using an Handler and sending data each second. All instructions are commented for you to understand how it works:
- You must now modify the AndroidManifest.xml file to let Android know this app has a service in it, otherwise it won't work. So you just have to add this line under the <application> section:
Receiving App
That's what you must do:
- Create a new android app without any activity
- Make a derived class of android.content.BroadcastReceiver
Here's an example code that explains the nexts parts:
- Compile it as a .jar file. Using Eclipse you can do it trhough Eclipse -> File -> Export -> Java -> Jar File, press next, check your project, uncheck all boxes in the right panel, uncheck jar compression, choose a destination file and press done.
Make Unity use our Java Receiver plugin
Importing the plugin
- Create this hierarchy of folders using the default Asset folder: Assets/Plugins/Android/
- Move your .jar file in it
Now, we have to modify the AndroidManifest.xml Unity uses to add some specifications for our receiver plugin to work properly. The easiest way to get this file is to build our Unity project to run on Android. When we build it, Unity create a manifest during compilation, that can be found in the Temp folder of our project. Copy it and move it next to the .jar file in the Android folder.
- Modify the manifest by adding these lines under the <application> section:
Using our plugin
I would like to thank Simon Philippe, a friend of mine, for having worked with me on this code.
As usual, excuse me for all my English mistakes, I'm not a native speaker.