SetData on intent prevents sendBroadcast from working?

I have a BroadcastReceiver registered to listen to the next action.

public static final String MY_ACTION = "com.blah.intent.action.DOSOMETHING"; 

And in my code I have

 Intent intent = new Intent(MY_ACTION); sendBroadcast(intent); 

If I use this, the broadcast is sent and gets fined, however, if I add Uri using setData before sending the broadcast, adding these lines.

 Uri uri = Uri.parse("/sdacrd/myfile"); intent.setData(uri); 

If I installed Data, the broadcast will not receive.

Can someone explain why setting intent data prohibits broadcasts?

+4
source share
2 answers

Android looks not only at ACTION, but also at the data type and schema. You must say that your recipient can get this type by calling addDataSchema () or addDataType () in an IntentFilter. If you just want to send String, why don't you use extra features?

+4
source

I agree with damluar.

From an Android developer: Add a new Intent data schema for mapping. If any schemas are included in the filter, then the Intent data must be either one of these schemas or a suitable data type. If the schemes are not included, then the intention will be consistent only if it does not contain data.

Conversely, if you add data to Intent, you must add the data type or schema to the intent filter.

+1
source

Source: https://habr.com/ru/post/1342632/


All Articles