I have a service in which I'm using a handler for getting the time of mediaplayer every millisecond.
But, when my service is not bound to any activity, i want to remove the callbacks from the handler in onUnbind(); method of the service.
When the Service gets bound to an activity, i want to reuse my handler by posting the same callbacks to it in on Rebind/onBind();
but, it does not work. need help!
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;
import java.io.IOException;
public class MediaService extends Service {
private static final String TAG = "MediaService";
IBinder myBinder = new MyBinder();
MediaPlayer mediaPlayer = new MediaPlayer();
boolean isPrepared = false;
MutableLiveData<Integer> currrentTime = new MutableLiveData<>();
MutableLiveData<Integer> mediaPlayerDuration = new MutableLiveData<>();
private Handler handlerTime;
@Nullable
@Override
public IBinder onBind(Intent intent) {
handlerTime = new Handler();
//readyTime method posts delayed runnable on the handler @recurring
readyTime();
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
handlerTime.removeCallbacks(curTimeRunnable);
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: MediaService Created");
}
class MyBinder extends Binder {
MediaService getService() {
return MediaService.this;
}
}
public void setPathAndPlay(String path) {
isPrepared = false;
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
isPrepared = true;
mp.start();
mediaPlayerDuration.postValue(mediaPlayer.getDuration());
}
});
} catch (IOException e) {
Log.d(TAG, "setPathAndPlay: Error");
e.printStackTrace();
}
}
public boolean isMediaPlaying() {
return this.mediaPlayer.isPlaying();
}
public void pause() {
mediaPlayer.pause();
}
public void resume() {
try {
mediaPlayer.start();
} catch (Exception e) {
}
}
public void seekTo(int millis) {
mediaPlayer.seekTo(millis);
}
public int getTimeTotal() {
return mediaPlayer.getDuration();
}
public int getCurrentPos() {
return mediaPlayer.getCurrentPosition();
}
Runnable curTimeRunnable = new Runnable() {
@Override
public void run() {
currrentTime.postValue(mediaPlayer.getCurrentPosition());
Log.d(TAG, "run: VALUE POSTED");
if (handlerTime != null) {
handlerTime.postDelayed(this, 1);
}
}
};
public void readyTime() {
if (handlerTime != null) {
handlerTime.postDelayed(curTimeRunnable, 1);
}
}
}
If you want to reuse the handler you have to do:
handler.removeCallbacksAndMessages(null);
In the docs for removeCallbacksAndMessages it says...
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.
I hope It helps you!