Issue
I'm working on an android project, and i've stumbled on a little problem. I made a rolling ball game from the unity tutorial page, and now i want to send the controls from an android application. This is crucial, i don't want the game to intercept the controls itself. I was using this tutorial:
http://jeanmeyblum.weebly.com/scripts--tutorials/communication-between-an-android-app-and-unity
I made a service in the android application, which i think is quite OK. I tested it if it's running in the debugger, and it is. Then i made the receiver according to the instruction, the same with the plugin part. When i run the game (which is in an activity in the android application), the receiver is ALMOST working, he gets the "text" value that i set in the static variable, but only the value that was in the declaration at the beginning (so i think that the plugin part is working correctly). The problem is that the onReceive() method is not being called, thus the "text" value wont change to "-1" which would move the ball in the opposite direction (the ball is always moving in the +1 direction).
Here is the code:
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();
}
}
}
And the Service:
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;
}
}
I also edited the manifests:
<service
android:name="com.example.MyService"
android:enabled="true" >
</service>
and the receiver:
<receiver android:name="com.example.receiver.MyReceiver">
<intent-filter>
<action android:name="com.example.package">
</action>
</intent-filter>
</receiver>
I can see the MyService in the phones settings, and the game gets the declared "text" value. The problem is the onReceive method i thin, which is never even called. Can anyone help me with this?
Solution
I left this project for some time, and came back to it yesterday. AND I MADE IT. The problem was that the UNITY receiver had to be also registered in the ANDROID manifest, in fact i think it has to be registered in both of the manifests. I hope this helps someone someday :D
Answered By - Rafal
Answer Checked By - Willingham (JavaFixing Volunteer)