Passing data from a broadcastreceiver to an activity

1

I have the following problem. I'm trying to send data from BroadcastReceiver to an activity.

in the onRecive () I have:

intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("valorPasar",dato);
context.startActivity(intent);

in the activity I have the following:

I'm trying to read the data variable (which is sent from the broadcastreceiver) sent inside (and capture it in the activity inside) onLocationChanged (Location location)

 Public void onLocationCHanged (Location location){

    Intent intent = new Intent();
    String mensaje = intent.getStringExtra("valorPasar")

 }

The problem is that the message variable does not load the value of "Passe value" defined in the Broadcastreceiver

    
asked by YANINA 13.06.2017 в 21:28
source

2 answers

1

You are getting the message of an empty Intent when doing new Intent()

You must use getIntent() that returns the intent for which the activity was called:

public void onLocationCHanged (Location location){

    Intent intencion = getIntent();
    Bundle extras = intencion.getExtras();
    String mensaje  = extras.getString("valorPasar");

 }
    
answered by 14.06.2017 / 13:53
source
-2

I recommend that you send the data in the bundle to your Activity that contains the onLocationChanged() method,

private String message;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mensaje = bundle.getStringExtra("valorPasar");        

}

When you get the value, you can use it within the onReceive() method:

  public void onReceive(Context context, Intent intent) {

    //Aquí se puede hacer uso de la variable mensaje.

  }
    
answered by 13.06.2017 в 21:36