Intent is null in the onRecieve () of a BroadcastReciver

0

I have a problem that I can not understand why it happens, I've been looking for more sites before asking but I have not found anything, here's the problem:

I create an Intent to send a broadcast in the following way:

Intent intent = new Intent(contexto,BroadcastReciver.class);
intent.putExtra("key","texto"));
contexto.sendBroadcast(intent);

This causes the BroadcastReciver class to start and enter the onRecive() method and I do the following:

@Override
public void onReceive(Context context, Intent intent) {
    String aux=intent.getStringExtra("key");
}

Well, when it comes there, that Intent that I recive has the Extras to null and I do not know why, I have verified that when creating the intent the data will not be empty and this is all correct until you reach the BroadcastReciver class.

EDITED: In manifest.xml I have it declared as follows:

 <receiver android:name=".utils.BroadcastReciver" />

utils is a package I have created myself.

    
asked by borjis 31.10.2016 в 14:01
source

1 answer

0

I have managed to do it in a somewhat "sloppy" way and that in my opinion is not a solution.

I have done the following:

Intent intent = new Intent(contexto,BroadcastReciver.class);
intent.setAction("texto");
contexto.sendBroadcast(intent);

And on the onReceive ():

@Override
    public void onReceive(Context context, Intent intent) {
    String aux= intent.getAction();
}

In this way, passing the String as an action I can get it later and use it for my purpose.

The problem with this is that you would be using a intent.setAction() method whose function is not what I am giving you, and another problem would be that only a String data could be passed to it.

That's why I say it's a "botched" and not a solution, but well, at the moment it works for me and lets me continue with my development.

    
answered by 31.10.2016 / 14:59
source