How to stop the service from Unity’s side with Unity’s Android plugin?

Articles about Plugin Development.

Often, the features you really want to add when you’re developing in Unity are in the native plugins on the OS side.

Plugin development is an inevitable part of game creation. I’ve managed to implement them, so I’ll start with the rudimentary ones in this article.

However, it’s my own style, so it may be wrong, inefficient, or not a smart way. If you have any suggestions for corrections, etc., I’d appreciate it if you could let me know in the comments.

The theme stops the Service from the Unity side.

I felt like it would be easy to do, but I couldn’t find it anywhere else, so I forced myself to make it.

Now let’s get down to business.

I understand that the basic idea is to start Sevice from an Activity, but in this design, I called the same Activity to start and end Sevice and identified the behavior by an id that I putExtra to Intent.

The first thing to do is to look at the Activity.

public class DemoActivity  extends Activity {

    //Activities to Launch Services

    Intent intent;
    private int st;
    public void onCreate(Bundle savedInstanceState){
        intent = new Intent(getApplicationContext(), DemoServise.class);
        Intent intent = getIntent();
        st = intent.getIntExtra("pre_id", 1);


    public void onStart() {

        //An ID of 1 is thrown at the start of DemoServise
        //and 0 at the end.
        if(st == 1){
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){


    public void onDestroy() {

As an example, we’ve written a DemoActivity.

Set Intent to 0 or 1 as “pre_id”. In this example, set the id to 1 when starting, store the id in st by getIntExtra and advance to startService or stopService by branching when onStart().

I also included a split with ForegroundService for background processing.

Unity’s code

Now let’s look at the Unity side of the code.

    public static void LocalServiceStart()
		    m_plugin.CallStatic("DemoLoad", context, 1);
        Debug.LogWarning("This asset is for Android only. It will not work inside the Unity editor!");

    public static void LocalServiceEnd()
		    m_plugin.CallStatic("StopService", context, 0);   
        Debug.LogWarning("This asset is for Android only. It will not work inside the Unity editor!");

Please put AndroidJavaObject in m_plugin.

In this example, we don’t call the Activity directly, but instead call a function in the Manager class once. Therefore, we store the Manager class in AndroidJavaObject.

The context is the

		unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
		context  = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

This is what’s in it.

This calls a static function, so it is CallStatic.

We’ve prepared for the start on top, and when it’s time to end the service, we call the function below to prepare to end the service.

In both cases, the second argument is Int. Enter 1 on the start side and 0 on the end side.

We see a manager class on the Java side.

    //Start an activity to run the service.
    public static void startDemoActivity(Context con,  int preid){
        Intent intent = new Intent(con, DemoActivity.class);

        intent.putExtra("pre_id", preid); //startup ID

        con.startActivity(intent); //Create activities and launch services from there.

    //Start an activity to stop the service.
    public  static void StopDemoActivity(Context con, int st){
        Intent intent = new Intent(con, DemoActivity.class);
        intent.putExtra("pre_id", st); //termination ID
        con.startActivity(intent); //Create and stop activities.

All calls from the Unity side are handled by the Manager class.

Add the preid received as an argument to Intent using putExtra.


This is how to stop the Java Service from Unity.

There may be a better way to do it, but the only thing I know of at the moment is this, and the other one, which is to make the timer object in the service ask for a static variable flag every 0.1 seconds, and then change the static variable flag from the manager class to stopSelf(). stuff.

I first came up with the latter but thought it wasn’t a very brilliant method, so I shifted to this method.

If you have any suggestions for a better way to do things, I’d love to hear about it in the comments.

So thank you for reading carefully.


Make a comment