Custom Filter Based on User Data

I want to transfer the intention using user data, and only the recipient from whom this user data should receive this intention, how can this be done?

this is how I convey the intention:

Intent intent = new Intent(); intent.setAction("com.example"); context.sendBroadcast(intent); 

and I define the broadcast receiver as follows:

  <receiver android:name="com.test.myReceiver" android:enabled="true"> <intent-filter> <action android:name="com.example"></action> </intent-filter> </receiver> 
+7
source share
2 answers

Configure the myReceiver class basically how anmustangs are hosted.

 public class myReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent intent) { //Do something } } 

You do not need to test the action of Intent because you have already filtered it based on what you included in the manifest, at least in this simple case. You may have other actions and filters, in which case you will need to have some kind of check on the intent received.

If you want a filter to filter data, the sdk documentation on intent filters covers this. I am not very versed in data types, so any example I would give would be relatively low. Anyway, here is a link to the manifest filter page: http://developer.android.com/guide/topics/manifest/intent-filter-element.html

And a specific page for the data item; http://developer.android.com/guide/topics/manifest/data-element.html

+2
source

If you have already defined your broadcast receive class, the next step is to register it with the Activity / Service that you want to receive for translation. Registration example:

  IntentFilter iFilter = new IntentFilter(); iFilter.addAction("com.example"); //add your custom intent to register //below is your class which extends BroadcastReceiver, contains action //which will be triggered when current class received the broadcast intent MyReceiver myReceiver = new MyReceiver(); registerReceiver(myReceiver, iFilter); //register intent & receiver 

If you destroy an action / service, for best practice, be sure to unregister the broadcast receiver for the same activity / service:

 @Override protected void onDestroy() { if(myReceiver!=null) unregisterReceiver(myReceiver) super.onDestroy(); } 

MyReceiver Class:

 public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent intent) { if(intent.getAction().equals("com.example")){ //DO SOMETHING HERE } } } 
0
source

All Articles