0

I had recently joined a new company and have taken over a project that has been on-going for years. The unfortunate problem with this picture is that some of the code has been badly maintained and the simplest way for me to describe the current situation is that the current implementations are overcomplicated.

I am currently in the midst of trying to simplify some of those operations (coding) so as to improve the overall app's performance and responsiveness. I have done my best not to change too much because as tempting as that is, that would involve too much work at this point.

I am trying to work towards the next release but I have to fix a few critical issues first.

One of those issues is the use of an Android service. At the present moment, at least according to what the previous developer(s) have done, they tried to create their services from scratch, which honestly didn't make sense because Android itself has provided a way to do it. I figured that perhaps using a built-in background service would suffice.

It has been a few years since I've actively worked on an Android project, but I still remember some of the things. The one thing I am trying to figure out is, whether it is possible to create an Android service class and then have it manage different kinds of operations in the background? Due to NDA, I can use the real code here, but I can give you an example:

// UserAccountService - Just an example from the docs.

public class UserAccountService extends Service {
    private Looper serviceLooper;
    private ServiceHandler serviceHandler

    private final class ServiceHandler extends Handler {
        public ServiceHandler (Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage (Message msg) {
            try {
                // do some work here
            } catch (InterruptedException ex) {
            }
            stopSelf(msg.arg1);
        }
    }

    @Override
    public void onCreate() {
        // same as the example from developer.android.com/develop/background-work/services#java
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    }

    @Override
    public IBinder onBind (Intent intent) {
    }

    @Override
    public void onDestroy() {
    }

    /**
     * Below I would have all the functions for performing some of the standard UserAccount related 
     * operations.
     */

     protected bool isAnonymousUserOnline() {
     }

     protected void autoLogin() {
     }

     protected void logout() {
     }

     protected User getUserById() {
     }

     protected User getAnonymousUser() {
     }
}
  1. Based upon the above brief overview and example, I was wondering, can I pass values via the intent object similar to navigating between activities and fragments? Looking at the provided coding example from the online docs, I assume it is possible.

  2. And then based upon the value(s) that can be gathered from the intent (within UserAccountService), I can do a switch-case block to filter the passed values and then run the different user account operations within the try {} block.

  3. Finally, of the different overwritten methods in the example, I can understand the purpose of onCreate for sure. It's the other two that I am curious about, like onStartCommand() and onBind(). What should we normally put in these functions?

What would be the best way to implement this UserAccountService? Are there any potential pitfalls that I need to be aware of?

2
  • Have you consulted Google for Developers (developer.android.com/develop/background-work/services) ? Commented Jun 20, 2024 at 10:36
  • Yeah, I was looking at the example that it provided. But the example only demonstrates a single class with 1 function/task. I am trying to create a class that has multiple functions/tasks that could be called at any time, and they should all be done in the background. So, using the Services approach, I'm just wondering how to do that. Also, where do I place those functions? Inside handleMessage(), or inside onStartCommand()? Commented Jun 21, 2024 at 17:01

1 Answer 1

0

in my app for the communication BluetoothLe part, for example, the communication is performed using a bound service and uses broadcasts to send the information from the service to activity.

See https://developer.android.com/develop/connectivity/bluetooth/ble/connect-gatt-server.

To send the information from the activity to service I simply call a service function. E.G. to notify the service whether the activity is active or paused

in the activity

bluetoothService!!.activityInPausedState()
bluetoothService!!.activityInActiveState()

in service

var activityIsPaused:Boolean = false
fun activityInPausedState() {
    activityIsPaused = true
}
fun activityInActiveState() {
    activityIsPaused = false
}

I hope I was a little helpful to you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.