GithubHelp home page GithubHelp logo

natsud77 / unity-background-service Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 30.0 144.56 MB

A simple project that shows how to create an Android service for Unity application working on background

Java 0.20% C# 98.00% HLSL 0.37% ShaderLab 1.43%

unity-background-service's Introduction

What is Unity Background Service?

Unity Background Service is a project that shows how to create an Android service for Unity application working on background. Usually Android service (especially in Unity apps) shuts down when we kill the app. It is impossible to make the service working fully on background. The only solution is to let the service work on foreground as a notification and then the users are able to hide that notification if they wants to. Specifically, this project shows the creation of a step counting service that works on background.

How to use?

doc_2021-03-02_12-38-25

How it works?


The main scene is located in Assets/Scenes. To see the example code go to BackgroundService class. Explanation of the methods:

  1. On Awake the SendActivityReference method is called. It creates the Unity Android class and Unity activity. Then it sends the current activity to the plugin (the .aar file should be located at Assets/Plugins/Android)
void SendActivityReference(string packageName)
    {
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        customClass = new AndroidJavaClass(packageName);
        customClass.CallStatic("receiveActivityInstance", unityActivity);
    }
  1. StartService and StopService methods respectively starts and stops the background service as well as GetCurrentSteps method simply gets the walked steps from plugin.
  2. SyncData method returns a string data that holds 3 variables separated with # symbol:
  • date of StartService method invocation
  • date of SyncData method invocation
  • count of steps walked during this period
public void SyncData()
    {
        string data;
        data = customClass.CallStatic<string>("SyncData");
        
        string[] parsedData = data.Split('#');
        string dateOfSync=parsedData[0] + " - " + parsedData[1];
        syncedDateText.text = dateOfSync;
        int receivedSteps = Int32.Parse(parsedData[2]);
        int prefsSteps = PlayerPrefs.GetInt(_prlayerPrefsTotalSteps,0);
        int prefsStepsToSave = prefsSteps + receivedSteps;
        PlayerPrefs.SetInt(_prlayerPrefsTotalSteps,prefsStepsToSave);
        totalStepsText.text = prefsStepsToSave.ToString();
        
        GetCurrentSteps();
    }

The entry point of the Android application is the Bridge class. Explanation of the methods:

  1. The receiveActivityInstance method is called when the SendActivityReference from Unity executes. It takes the Unity activity to know where to start the background service in the future. Also it checks if the permission for activity recognition is granted and asks for the permission if it is not (this logic is implemented for Android API 28 and above).
public static void receiveActivityInstance(Activity tempActivity) {
        myActivity = tempActivity;
        String[] perms= new String[1];
        perms[0]=Manifest.permission.ACTIVITY_RECOGNITION;
        if (ContextCompat.checkSelfPermission(myActivity, Manifest.permission.ACTIVITY_RECOGNITION)
                != PackageManager.PERMISSION_GRANTED) {
            Log.i("PEDOMETER", "Permision isnt granted!");
            ActivityCompat.requestPermissions(Bridge.myActivity,
                    perms,
                    1);
        }
    }
  1. GetCurrentSteps is called when the GetCurrentSteps is called from Unity.

unity-background-service's People

Contributors

nintendaii avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.