Reputation: 79
I'm a newbie in Android.Here, I have a Checkbox in my Activity. What I actually want is, on enabling the Checkbox ,my BroadcastReceiver needs to respond. So for that what do I do? How is it done.
Can anyone give a sample code for doing that?
Can you please tell me whether I can do this:
public MyActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
.....
.....
//Check a condition and if its true declare a BroadcastReceiver like this here
class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// do something
}
}
Thanks in Advance
Upvotes: 1
Views: 444
Reputation: 3858
try this
if(_cb.isChecked()){
startService(serviceIntent);
registerReceiver(broadcastReceiver, new IntentFilter(YourService.BROADCAST_ACTION));
}else{
stopService(serviceIntent);
unregisterReceiver(broadcastReceiver);
}
and post your code..
Upvotes: 2
Reputation: 39807
That's kind of like asking "How do I make my telephone ring when I turn on the television?". Of course the answer here is that something must be monitoring the television and, when it comes on, must dial your telephone number. Or, in your case, you need to have a listener that is called when your checkbox is checked, and in response it must broadcast an Intent whose action and filters match what your broadcast receiver is configured to receive.
Upvotes: 1