0

I am developing an android app which calculates screen to face distance which has one camera preview in which app detect the eyes and calculates the distance and show distance above the preview and adjust the android system font according to distance calculated. Currently the app calculates distance when i run it. I want this app to continuously calculate the distance in background and change the font accordingly but now its not calculating distance in background.

I expect that app should not pause in background and calculate the distance even in background and change the system font accordingly.

5 Answers5

0

This sounds like a typical example of a task you want to do in a background Service. To quote the description from the Android documentation:

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
FreddaP
  • 121
  • 1
  • 5
0

Use AsyncTask and you can achieve this by using its core methods like doInBackground and progressUpdate()

Prinkal Kumar
  • 3,196
  • 2
  • 10
  • 15
  • But i am using camera preview in that activity and then calculating distance so is it possible to do this using service and if yes then could u please share any example related to this – Yash Agrawal Mar 28 '19 at 11:32
0

You can use WorkManager. WorkManager is best for background jobs. Refer following links

https://developer.android.com/topic/libraries/architecture/workmanager (Introduction)

https://github.com/googlesamples/android-architecture-components/tree/master/WorkManagerSample (Examaple)

Sohel S9
  • 246
  • 1
  • 15
0

Just like what @FreddaP said, for me, I would use a Service in here because it seems like it only needs to go a single task, which is calculating the distance of the phone from a face, over and over again.

If you want to be a bit adventurous, use native code to increase it's speed (not like I know how to make it work though).

This SO answer seems to be what you'll need. Sorry I haven't used the camera before in my apps.

rmanalo
  • 342
  • 3
  • 23
  • But i am using camera preview in that activity and then calculating distance so is it possible to do this using service and if yes then could u please share any example related to this – Yash Agrawal Mar 28 '19 at 11:31
0

I think that this particular problem (to use the camera without preview) has already been addressed in this post

FreddaP
  • 121
  • 1
  • 5