Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast Receiver in fragment not receive intent from service

I have a fragment and a service as below:

I register and unregister LocalbroadcastManager in file Fragment.java

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:

ffmpegService.java

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.

like image 410
Sinh Ho Avatar asked Mar 20 '14 18:03

Sinh Ho


1 Answers

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.

like image 150
Sinh Ho Avatar answered Sep 19 '22 14:09

Sinh Ho