我希望能够通过两个按钮启动和停止广播接收器。我已经在两个按钮监听器中编写了此代码。
代码如下:
在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");
}
});
当新的来电到达时,听筒仍在响铃。