title: Make a Unity Android Activity Communicate With Android Service
body: <p>I am trying to set up a background service to use with a game I am developing in Unity. At this stage I want a variable to update and be displayed in unity after the service has been started. Currently, the variable updates every second and is displayed on a toast, but it cannot be seen or used by unity. </p>
  
  <p>I have tried setting up a BroadcastReceiver based on this tutorial <a href="http://jeanmeyblum.weebly.com/scripts--tutorials/communication-between-an-android-app-and-unity" rel="nofollow noreferrer">http://jeanmeyblum.weebly.com/scripts--tutorials/communication-between-an-android-app-and-unity</a>
  however it seems outdated and others are also struggling with this issue.</p>
  
  <p>Unity code:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PluginWrapper : MonoBehaviour {
  
  TextMesh textmesh;
  
  AndroidJavaClass unityClass;
  AndroidJavaObject unityActivity;
  AndroidJavaClass customClass;
  // Use this for initialization
  void Start () {
      //AndroidJNIHelper.debug = true;
      textmesh = GetComponent&lt;TextMesh&gt;();  
      sendActivityReference("com.example.locationplugin.StatusCheckStarter");
      customClass.CallStatic("createInstance");
  }
  
  void Update() {
      print(customClass.GetStatic&lt;string&gt;("text"));
  }
  
  public void clickButton() {
      startService();
      customClass.CallStatic("makeToast");
  }
  
  public void decreaseCount() {
      stopService();
  }
  
  void sendActivityReference(string packageName) {
      unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      unityActivity = unityClass.GetStatic&lt;AndroidJavaObject&gt; ("currentActivity");
      customClass = new AndroidJavaClass(packageName);
      customClass.CallStatic("receiveActivityInstance", unityActivity);
  }
  
  void startService() {
      customClass.CallStatic("StartCheckerService");
  }
  
  void stopService() {
      customClass.CallStatic("StopCheckerService");
  }
  }
  </code></pre>
  
  <p>Android Plugin:</p>
  
  <pre><code>    package com.example.locationplugin;
  import android.app.Service;
  import android.content.Intent;
  import android.os.Handler;
  import android.os.IBinder;
  import android.support.annotation.Nullable;
  import android.widget.Toast;
  
  public class LocationPlugin extends Service {
  
  public LocationPlugin ref;
  public static int number = 0;
  private final Handler handler = new Handler();
  
  private Runnable sendData = new Runnable() {
      public void run() {
          number++;
  
          send(number);
  
          Toast.makeText(ref, "number is: " + number, Toast.LENGTH_LONG).show();
  
          // In our case we run this method each 2 second with postDelayed
          handler.removeCallbacks(this);
          handler.postDelayed(this, 1000);
      }
  };
  
  public void send(int num) {
      Intent sendIntent = new Intent();
  
      sendIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
      sendIntent.setAction("com.example.locationplugin.LocationPlugin");
      sendIntent.putExtra(Intent.EXTRA_TEXT, num);
      sendBroadcast(sendIntent);
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startID) {
      // number = 0;
      ref = this;
      Toast.makeText(this, "service started", Toast.LENGTH_LONG).show();
  
      handler.removeCallbacks(sendData);
      handler.postDelayed(sendData, 1000);
      return START_STICKY;
  }
  
  public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "service stopped", Toast.LENGTH_LONG).show();
      handler.removeCallbacks(sendData);
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  }
  </code></pre>
  
  <p>Broadcast Receiver: </p>
  
  <pre><code>    package com.example.locationplugin;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.Toast;
  
  public final class StatusCheckStarter extends BroadcastReceiver{
  static Context myActivity;
  private static StatusCheckStarter instance;
  public static LocationPlugin loc = new LocationPlugin();
  public static String text = "hello world";
  
  public static void makeToast(){
      Toast.makeText(myActivity, text, Toast.LENGTH_LONG).show();
  }
  
  public static void receiveActivityInstance(Context tempActivity) {
      myActivity = tempActivity;
  }
  
  public static void StartCheckerService() {
      myActivity.startService(new Intent(myActivity, LocationPlugin.class));
  }
  
  public static void StopCheckerService() {
      myActivity.stopService(new Intent(myActivity, LocationPlugin.class));
  }
  
  
  @Override
  public void onReceive(Context context, Intent intent) {
      Toast.makeText(myActivity, "received!!!!", Toast.LENGTH_LONG).show();
  
      String sentIntent = intent.getStringExtra(Intent.EXTRA_TEXT);
      text = "got into onreceive";
      if (sentIntent != null) {
          // We assigned it to our static variable
          text = sentIntent;
      }
  }
  
  public static void createInstance()
  {
      if(instance ==  null)
      {
          instance = new StatusCheckStarter();
      }
  }
  }
  </code></pre>
  
  <p>Android Manifest:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.unity3d.player"
  xmlns:tools="http://schemas.android.com/tools"
  android:installLocation="preferExternal"
  android:versionCode="1"
  android:versionName="1.0"&gt;
  &lt;supports-screens
      android:smallScreens="true"
      android:normalScreens="true"
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true"/&gt;
  
  &lt;application
      android:theme="@style/UnityThemeSelector"
      android:icon="@drawable/app_icon"
      android:label="@string/app_name"&gt;
      &lt;activity android:name="com.unity3d.player.UnityPlayerActivity"
                android:label="@string/app_name"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
          &lt;meta-data android:name="unityplayer.UnityActivity" android:value="true" /&gt;
      &lt;/activity&gt;
  
    &lt;receiver android:name="com.example.locationplugin.StatusCheckStarter"&gt;
      &lt;intent-filter&gt;
        &lt;action android:name="com.example.locationplugin.LocationPlugin" &gt;&lt;/action&gt;
      &lt;/intent-filter&gt;
    &lt;/receiver&gt;
  
  
    &lt;service android:name="com.example.locationplugin.LocationPlugin"/&gt;
  
  &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>