Skip to content

Instantly share code, notes, and snippets.

@chandruark
Last active May 30, 2018 09:37
Show Gist options
  • Save chandruark/c721be8cba208898158e6158b062a29d to your computer and use it in GitHub Desktop.
Save chandruark/c721be8cba208898158e6158b062a29d to your computer and use it in GitHub Desktop.
public class SampleActivity extends AppCompatActivity {
/**
* Sample Snippet for Registering Receiver
*/
MyBroadcastReceiver myBroadCastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
myBroadCastReceiver=new MyBroadCastReceiver();
}
@Override
protected void onResume() {
/**
* Here you can register the receiver
*/
registerBroadcastReceiver();
super.onResume();
}
private void registerBroadcastReceiver() {
/**
* Defining Intent Filters
*/
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("android.intent.action.BOOT_COMPLETED");
intentFilter.addAction("android.intent.action.QUICKBOOT_POWERON");
registerReceiver(myBroadCastReceiver,intentFilter);
}
/**
* Unregistering Receiver is not used in our case , because
* we need to set repeatly ,even though after app ondestroy state
*/
private void unregisterBroadcastReceiver() {
/**
* Unregistering Receiver
*/
unregisterReceiver(myBroadCastReceiver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment