我想通过两个按钮启动和停止广播接收器。我已经在两个按钮侦听器中为此编写了代码。
代码如下:-
在 startButton 中,我编写了注册接收器的代码,如下所示
startCallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCallBtn.setVisibility(View.INVISIBLE);
stopCallBtn.setVisibility(View.VISIBLE);
//register call receiver.
registerReceiver(callReceiver, new IntentFilter(
"android.intent.action.CALL"));
System.out.println("Registered Call Receiver Perfectly");
}
});
在停止按钮中,我将停止接收器的调用写为
stopCallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopCallBtn.setVisibility(View.INVISIBLE);
startCallBtn.setVisibility(View.VISIBLE);
stopService(new Intent(MainActivity.this, SpeakOut.class));
FileOutputStream fos;
//Unregister sms receivers
unregisterReceiver(callReceiver);
System.out.println("Unregistered Call Broadcast Receiver");
}
});
接收新呼叫时,侦听器仍在呼叫