Using intent filters with Android broadcast radios

please, how to use broadcastReceiver correctly with Intent filters. In my android_manifest.xml file, I have the following lines:

<activity android:name=".DataDisplayActivity" android:theme="@android:style/Theme.Holo.NoActionBar" android:icon="@drawable/icon_3d" android:label="AdvancedHyperXPositiveSuperFluousApp"> <intent-filter> <action android:name="com.simekadam.blindassistant.UPDATE_GPS_UI"/> <action android:name="com.simekadam.blindassistant.UPDATE_CONTEXT_UI"/> </intent-filter> <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

And in this exercise, I installed the receiver using this function

  registerReceiver(broadcastReceiver, null); 

It does not work in null, obvi needs to set IntentFilter, and I can add it as a function parameter, but I ask how to use it with XML intent filters. Thanks for your help.

Teaser: I really got it working with a built-in intent, but I ask how to get it to work with the intent set in XML.

+6
source share
2 answers

you do not need to define intent filters in your xml when you use registerReceiver to receive translations.

In your case, you must create a class that extends to BroadcastReceiver, and then define this class file in the Android manifest file. eg:

class file:

 package your.package.name; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyCustomReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals("com.simekadam.blindassistant.UPDATE_GPS_UI")){ //do something } else if(action.equals("com.simekadam.blindassistant.UPDATE_CONTEXT_UI")){ //do something } } } 

and addition in the manifest:

 <receiver android:name=".MyCustomReceiver" android:enabled="true"> <intent-filter> <action android:name="com.simekadam.blindassistant.UPDATE_GPS_UI" /> <action android:name="com.simekadam.blindassistant.UPDATE_CONTEXT_UI" /> </intent-filter> </receiver> 
+16
source

You can register your receiver and intent filters in the manifest.xml file as described above, or you can do both of these things dynamically:

 Myreceiver reMyreceive = new Myreceiver(); IntentFilter filter = new IntentFilter("actionname"); registerReceiver(reMyreceive, filter); 

where myreceiver will be your extended broadcast receiver of your class. You can view the full example here .

+5
source

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


All Articles