1

In my Fragment, I have a button, when button pressed, I broadcast a custom intent in following way:

package com.my.store.fragments.shopping;

public class ShoppingFragment extends Fragment{
    ...
    @Override
    public void onStart(){
       super.onStart()

       myButton.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v){
                broadcastMyIntent(v);
             }
       });
    }

    public void broadcastMyIntent(View view){
     Intent intent = new Intent();
     intent.setAction("com.my.store.fragments.shopping.CUSTOM_INTENT");
     getActivity().sendBroadcast(intent);
    }
}

Then, I defined a broadcast receiver:

package com.my.store.utils;

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Receive my intent", Toast.LENGTH_LONG).show();
    }
}

I register the receiver in AndroidManifest.xml:

<application
    ...>
    <activity ...>
       ...
    </activity>

    <!--this is the receiver which doesn't work-->
    <receiver android:name="com.my.store.utils.MyReceiver"> 
          <action android:name="com.my.store.fragments.shopping.CUSTOM_INTENT"/>
    </receiver>

    <!--I have another receiver here, it is working fine-->
   <receiver android:name="com.my.store.utils.AnotherReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>

I run my app, when I press the button, my receiver isn't called. Why?

Mellon
  • 37,586
  • 78
  • 186
  • 264

3 Answers3

2

You forgot to surround your <action> element with an <intent-filter> container.

woot
  • 3,671
  • 4
  • 26
  • 39
  • I didn't forget that, I just didn't include because this link suggests me not to do that (because I got the same warning as the link mentioned): http://stackoverflow.com/questions/16112470/android-exported-receiver-does-not-require-permission-on-receivers-meant-to – Mellon Jul 08 '13 at 14:07
  • In the example you showed the answer is stating only to do that if you're using an `Intent` with the class name rather than an action string. You can do that by using Intent it = new Intent(context, MyReceiver.class) - So either you use this syntax or you surround your action element with an intent-filter container. – woot Jul 08 '13 at 14:13
0

AndroidManifest.xml

<!--this is the receiver which doesn't work-->
<receiver android:name="com.my.store.utils.MyReceiver"> 
  <intent-filter>
   <action android:name="com.my.store.fragments.shopping.CUSTOM_INTENT"/>
  </intent-filter>
</receiver>
Paul
  • 26,170
  • 12
  • 85
  • 119
iStar
  • 1,112
  • 12
  • 20
0

Try this

<receiver android:name="com.my.store.utils.MyReceiver"> 
   <intent-filter>
      <action android:name="com.my.store.fragments.shopping.CUSTOM_INTENT"/>
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</receiver>
mach
  • 8,315
  • 3
  • 33
  • 51