Skip to content

Instantly share code, notes, and snippets.

@akalipetis
Created February 16, 2014 23:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akalipetis/9042015 to your computer and use it in GitHub Desktop.
Save akalipetis/9042015 to your computer and use it in GitHub Desktop.
<receiver android:name="com.akalipetis.android.testing_demo.MyReceiver">
<intent-filter>
<action android:name="my.custom.action"/>
</intent-filter>
</receiver>
public class MainActivity extends Activity {
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Act when in foreground here
abortBroadcast();
}
};
private IntentFilter mFilter = new IntentFilter("my.custom.action");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
}
@Override
protected void onStart() {
super.onStart();
registerReceiver(mReceiver, mFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mReceiver);
}
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Implement action when not in foreground here
}
}
@Manikkumar1988
Copy link

Can you also add on how to send the broadcast?

sendOrderedBroadcast()

@Manikkumar1988
Copy link

Intent intent = new Intent();
intent.setAction("my.custom.action");
sendOrderedBroadcast(intent,null);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment