In my app which is a mediaplayer, i want to know if my Service is running or not.
The reason i want to know this is because i only want to bind my service when it's already running.
When my app is opened and a song is clicked i call the method startMusicService which checks if the service is already bound or not, if not then i call the method bindMusicService.
in bindMusicService i check whether mServiceIsBound is true/false, if false i then Bind my Service which then calls onServiceConnected.
in onServiceConnected i check if serviceAlreadyRunning is false, if so then i start playing my song.
Everything works fine, but i'm sure there is a better and more efficient way achieving the same thing.
I did it this way because if i bind my service let's say in onCreate, it already starts playing a song without selecting one.
I only want my service to be started when i select a song to play.
public void bindMusicService(boolean alreadyRunning){
//boolean alreadyRunning: if True, then we don't call startActionPlay() in onServiceConnected() because Service is already running.
serviceAlreadyRunning = alreadyRunning;
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
if (!mServiceIsBound) {
try {
mediaPlayerServiceIntent = new Intent(context, MediaPlayerService.class);
context.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.e("Main", "An exception was caught: Service is not bound!");
}
}
}
public void startMusicService(int songPos, ArrayList<Song> songs){
songIndex = songPos;
songList = songs;
if (mServiceIsBound) {
mediaPlayerService.startActionPlay(context, songList, songIndex);
}else{
bindMusicService(false);
Log.i("Main", "startMusicService(): Service was not bound yet, binding Service...");
}
Code in onServiceConnected
public void onServiceConnected(ComponentName name, IBinder service) {
MediaPlayerService.MusicBinder binder = (MediaPlayerService.MusicBinder)service;
mediaPlayerService = binder.getService();
if (!serviceAlreadyRunning) {
mediaPlayerService.startActionPlay(context, songList, songIndex);
}
mServiceIsBound = true;
Intent intent = new Intent(Constants.ACTIONS.BROADCAST_SERVICE_BOUND);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Log.i("Main","MediaPlayerService is connected!");
}
Why is the code below considered bad? Alot of posts say that ActivityManager for checking running service only should be used for debugging purposes and not for release.
in onResume() i have this code:
if (isMyServiceRunning(MediaPlayerService.class)){
if (!Main.getInstance().mServiceIsBound) {
Main.getInstance().bindMusicService(true);
Log.i(TAG, "Service already running but not bound!");
}
Log.i(TAG, "Service is running!");
}
isMyServiceRunning method
private boolean isMyServiceRunning(Class<?> serviceClass){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager != null){
for (ActivityManager.RunningServiceInfo serviceInfo: activityManager.getRunningServices(Integer.MAX_VALUE)){
if (serviceClass.getName().equals(serviceInfo.service.getClassName())){
return true;
}
}
}
return false;
}