0

I want to know how I can send an object of a class in fragment ,to broadcast receiver.

When I call object directly with fragment object , it is always null.

Example :

Here I create a receiver class. this class handles custom Notification layout events . everything works correct except for a record.pause(). MediaRecorder object is always null in this receiver ,while in the Sample_Fragment which records audio it is not. hence recording does not stop.

Receiver Class :

public class Sample_Receiver extends BroadcastReceiver {

Sample_Fragment fragment = new Sample_Fragment();

 @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("pause".equalsIgnoreCase(action)) {
            
         record.pause();

                         
}

    }
}

Update :

I could fix the problem through inner BroadcastReceiver.

A.G
  • 87
  • 1
  • 8
  • Provide sample code... What you did. First you've to write parcalable or Serization – Gobu CSG Aug 11 '22 at 15:04
  • What you're doing? Sample_Fragment fragment = new Sample_Fragment(); First you need to understand android basics. Here you can't create fragment instance. Follow this https://stackoverflow.com/questions/10032480/how-to-pass-data-to-broadcastreceiver – Gobu CSG Aug 11 '22 at 15:41
  • Oh! I think I was crazy when I was typing that time. I was trying to solve the issue for hours and sometimes I did not know what I am doing. I have a long way to go . however, you said that I should use serization but the error is cannot cast from mediarecorder to serializable . I think I should work on a Service class instead of this path.Anyway,thanks for your help. – A.G Aug 11 '22 at 18:02

0 Answers0