我正在尝试让应用程序读出来电者的姓名。我认为注册BroadcastReceiver
的方式存在问题,但没有显示错误。
private void initializeCallReceiver(){
callReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
String caller = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
speaker.speak("Call from " + caller);
}
}
};
}
private void registerCallReceiver() {
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.EXTRA_STATE_RINGING");
registerReceiver(callReceiver,intentFilter);
}
Oncreate方法
toggle = (ToggleButton)findViewById(R.id.speechToggle);
smsText = (TextView)findViewById(R.id.sms_text);
smsSender = (TextView)findViewById(R.id.sms_sender);
toggleListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
if(isChecked){
speaker.allow(true);
speaker.speak(getString(R.string.start_speaking));
}else{
speaker.speak(getString(R.string.stop_speaking));
speaker.allow(false);
}
}
};
toggle.setOnCheckedChangeListener(toggleListener);
checkTTS();
initializeCallReceiver();
registerCallReceiver();
}
答案 0 :(得分:0)
这是我得到的解决方案。它现在有效。
private void registerCallReceiver(){
IntentFilter intentFilter = new IntentFilter(“android.intent.action.PHONE_STATE”);
registerReceiver(callReceiver,IntentFilter的);