MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
The string itself does not matter, just need to be the same in all places and unique, I use fully qualified name of the constant.
The receiver:
package com.mypackage.receivers;
public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String ACTION_CUSTOM = "com.mypackage.receivers.MyBroadcastReceiver.ACTION_CUSTOM";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_CUSTOM.equals(intent.getAction())) {
// do custom action
}
}
}
To broadcast the intent:
sendBroadcast(new Intent(MyBroadcastReceiver.ACTION_CUSTOM));