I have a media player service, and I need the fragment UI to be updated or in sync with the service data. Currently I am using a Broadcast to send and receive the data. But my question is, is there a better way to do this in android?
In Service:
private void startBroadcast() {
if(broadcastThread!= null && !broadcastThread.isInterrupted())
broadcastThread.interrupt();
broadcastThread = new Thread(){
@Override
public void run() {
try {
while(!isInterrupted()) {
Intent intent = new Intent(FILTER);
intent.putextra("foo",1);
sendBroadcast(intent);
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
broadcastThread.start();
}
In Fragment:
private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int foo= intent.getIntExtra("foo", 0);
// bla bla
}
};
.
.
@Override
public void onResume() {
super.onResume();
Objects.requireNonNull(getActivity()).registerReceiver(serviceReceiver ,FILTER);
}
you can use Bind Service to connect service to your activity and after that update all of your fragment with that
bindService(oIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
private ServiceConnection mServiceConnection =
new ServiceConnection(){
public void onServiceConnected(
ComponentName cName, IBinder service){
MyBinder binder = (MyService.MyBinder) service;
mService = binder.getService();
// Get a reference to the Bound Service object.
mServiceBound = true;
}
public void onServiceDisconnected(ComponentName cName){
mServiceBound= false;
}
};
and after you want change your activity you should unbind connection
if (mServiceBound){
unbindService(mServiceConnection);
mServiceBound = false;
}
I had a similar problem with MediaPlayer, I had to update UI component in every seconds, too. I used Runnable + Handler combo, like this:
//...
private Handler mHandler;
private Runnable mSeekBarUpdater
//...
@Override
protected void onStart() {
super.onStart();
//...
int result;
// ...
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMediaPlayer.setOnCompletionListener(this);
// Start playing and set isPlaying to true. This attribute is required on rotating screen
mMediaPlayer.start();
isPlaying = true;
/**
* Preparing Handler object and a Runnable in order to
* update SeekBar status
*/
if (mHandler == null) {
mHandler = new Handler();
}
if (mSeekBarUpdater == null) {
mSeekBarUpdater = new Runnable() {
@Override
public void run() {
if (mMediaPlayer != null && mHandler != null) {
if (mMediaPlayer.isPlaying()) {
// Refreshing SeekBar position
mCurrentPosition = mMediaPlayer.getCurrentPosition();
mSeekBar.setProgress(mCurrentPosition);
// .. put your code here in order to refresh your UI components
}
// Refreshing on every second
mHandler.postDelayed(this, 1000);
}
}
};
}
PlayerActivity.this.runOnUiThread(mSeekBarUpdater);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.