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.
The main scene is located in Assets/Scenes. To see the example code go to BackgroundService class. Explanation of the methods:
- 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);
}
StartService
andStopService
methods respectively starts and stops the background service as well asGetCurrentSteps
method simply gets the walked steps from plugin.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:
- The
receiveActivityInstance
method is called when theSendActivityReference
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);
}
}
GetCurrentSteps
is called when theGetCurrentSteps
is called from Unity.