1

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

4

2 回答 2

2

尝试这个

if(_cb.isChecked()){
  startService(serviceIntent);
  registerReceiver(broadcastReceiver, new IntentFilter(YourService.BROADCAST_ACTION));
}else{
  stopService(serviceIntent);
  unregisterReceiver(broadcastReceiver);
}

并发布您的代码..

于 2012-04-20T11:48:37.733 回答
1

这有点像问“我如何在打开电视时让电话响起?”。当然,这里的答案是必须有东西在监视电视,并且当它打开时,必须拨打您的电话号码。或者,在您的情况下,您需要在选中复选框时调用一个侦听器,并且作为响应,它必须广播一个 Intent,其操作和过滤器与您的广播接收器配置接收的内容相匹配。

于 2012-04-20T11:35:47.497 回答