我正在做一个应用程序,它会在铃声响4秒后说出来电者姓名。问题是它在两三声响起后会说出来电者姓名。问题是,即使呼叫处于摘机状态或闲置状态,我也会不停地说出呼叫者姓名。我不想这样做。
接收广播接收器的代码是
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals( TelephonyManager.CALL_STATE_OFFHOOK))
{
System.out.println("fjkerj");
}
else if(state.equals( TelephonyManager.CALL_STATE_IDLE))
{
}
else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Entered Receiver");
final String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (amanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
System.out.println("Silent");
break;
case AudioManager.RINGER_MODE_VIBRATE:
System.out.println("Vibrate");
break;
case AudioManager.RINGER_MODE_NORMAL:
amanager.setStreamVolume(AudioManager.STREAM_RING,1, 0);
Handler handler=new Handler();
Runnable r=new Runnable()
{
public void run()
{
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent IncomingNumberIntent = new Intent(context1,SpeakOut.class);
IncomingNumberIntent.putExtra("PhoneNumber", phonenumber);
context1.startService(IncomingNumberIntent);
}
}
};
handler.postDelayed(r, 3000);
如果呼叫正在振铃并且配置文件正常,它将转到服务并说出名称但是 即使电话被摘机,应用也会说出这个名字的麻烦。
答案 0 :(得分:1)
将您的代码更改为
int state = bundle.getInt(TelephonyManager.EXTRA_STATE);
switch (state)
{
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("fjkerj");
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
// Your code goes here
}
从run()方法中删除if语句
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))