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() {
}
}
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.
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.
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()andonBind(). 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?