使来电广播接收者说出来电者的姓名

时间:2015-07-24 10:54:17

标签: android text-to-speech

我正在尝试制作一个来电广播接收器,它可以说出呼叫者的姓名。对于如何使用文字转语音(TTS)来实现这一点,我感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

AndroidManifest.xml

中添加Receiver
   <receiver android:name=".IncomingCall">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
   </receiver>

添加权限

   <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

创建监听器

private class MyPhoneStateListener extends PhoneStateListener {

                    public void onCallStateChanged(int state, String incomingNumber) {

                       // Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);


                       // state = 1 means when phone is ringing
          if (state == 1) {

             String msg = " New Phone Call Event. Incomming Number : "+incomingNumber;
             int duration = Toast.LENGTH_LONG;
             Toast toast = Toast.makeText(pcontext, msg, duration);
                            toast.show();

                      }
                }
          }

添加 BroadCastReceiver

    public class IncomingCall extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

    try {
               // TELEPHONY MANAGER class object to register one listner
                TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);

                //Create Listner
                MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

                // Register listener for LISTEN_CALL_STATE
                tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {

            Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

            if (state == 1) {

                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(pcontext, msg, duration);
                toast.show();

            }
        }
    }
}
相关问题