0

我正在做一个应用程序,它会在铃声响起 4 秒后说出来电者姓名。问题是它会在两三声响铃后说出来电者姓名。问题是即使呼叫处于摘机或空闲状态,它也会一直说出呼叫者的姓名,我不希望这样。

广播接收器的 onreceive 代码是

  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);       

如果呼叫在响铃并且配置文件正常,它会转到服务并说出名称,但即使呼叫摘机,应用程序也会说出名称的麻烦。

4

1 回答 1

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))
于 2013-03-19T04:58:28.697 回答