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?