I have a fragment and a service as below:
I register and unregister LocalbroadcastManager in file Fragment.java
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ffmpegService.PROGRESS);
intentFilter.addAction(ffmpegService.DONE);
LocalBroadcastManager.getInstance(getActivity()).
registerReceiver(receiver, intentFilter);
}
@Override
public void onStop() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
super.onStop();
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase(ffmpegService.PROGRESS)){
....
}
}
In ffmpegService class, I send broadcast to fragment in 1 handler as below:
private Handler handler;
private Context mContext;
public void onCreate() {
mContext = this.getApplicationContext();
}
@Override
public void onStart(Intent intent, int startid) {
handler = new Handler();
handler.postDelayed(workTask, 3000);
}
private Runnable workTask = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(PROGRESS);
intent.putExtra("progress", 90);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}
}
But in running, the fragment did not receive anything from service broadcasting.
Any help would be appreciated!
Thanks.
I found the root cause. Because my service is running in own process.
<service
android:name="com.media.ffmpeg.ffmpegService"
android:enabled="true"
android:process="com.media.ffmpegservice">
</service>
I deleted android:process="com.media.ffmpegservice" and it works ok.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With