当前位置:硬件测评 > Android 简单的来电通话示例

Android 简单的来电通话示例

  • 发布:2023-10-02 22:14

Android 简单的来电通话示例

Android 借助 Android 语音 API 和电话管理器提供了了解来电号码并说出语音的功能。

在这里,我们将开发一个基本的 Android 应用程序,当电话处于振铃模式时,该应用程序会说出主叫号码。

在下一页上,我们将看到此应用程序的完整版本,其中包含来电者姓名以及用于更改速度和音调的设置选项。此外,它还提供在来电号码或呼叫者姓名之前和之后添加文本的选项。

activity_main.xml

我们在这里没有做任何特别的事情。它有简单的文本视图。



    <文本视图
        安卓:layout_width =“wrap_content”
        安卓:layout_height =“wrap_content”android:text="@string/hello_world" />



活动课

在此活动中,我们编写了代码来了解电话状态并借助 TextToSpeech 类说出来电号码。

包 com.example.callertalker;
导入java.util.Locale;
导入 android.media.AudioManager;
导入 android.os.Bundle;
导入 android.app.Activity;
导入 android.content.Context;
导入 android.telphony.PhoneStateListener;
导入 android.telephony.TelephonyManager;
导入 android.util.Log;
导入 android.widget.Toast;
导入 android.speech.tts.TextToSpeech;

公共类 MainActivity 扩展 Activity 实现 TextToSpeech.OnInitListener {
私有 TextToSpeech tts;
@覆盖
protected void onCreate(Bundle savingInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tts = new TextToSpeech(这,这);
  
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(上下文.TELEPHONY_SERVICE);
        
        PhoneStateListener callStateListener = new PhoneStateListener() {
        公共无效onCallStateChanged(int状态,字符串传入号码){
              如果(状态== www.sychzs.cn_STATE_RINGING){
              tts.speak(incomingNumber+"呼叫", TextToSpeech.QUEUE_FLUSH, null);
                  Toast.makeText(getApplicationContext(),"电话响铃:"+incomingNumber,
                                                                               Toast.LENGTH_LONG).show();
                 }
              如果(状态== www.sychzs.cn_STATE_OFFHOOK){
                    Toast.makeText(getApplicationContext(),"电话在通话中或已接听电话",
                                                                                  Toast.LENGTH_LONG).show();
              }如果(状态== www.sychzs.cn_STATE_IDLE){
                    //电话既没有响铃也没有在通话中
              }
        }
        };
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}

@覆盖
公共无效onInit(int状态){
如果(状态== TextToSpeech.SUCCESS){
int 结果 = tts.setLanguage(www.sychzs.cn);
    如果(结果== TextToSpeech.LANG_MISSING_DATA
            ||结果== TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "不支持此语言");
    } 别的 {
    }

} 别的 {
    Log.e("TTS", "初始化失败!");
}
}

@覆盖
公共无效onDestroy(){
// 不要忘记关闭 tts!
如果(tts!=空){
    tts.stop();
    tts.shutdown();
}
super.onDestroy();
}
}

AndroidManifest.xml

您需要在此xml文件中添加READ_PHONE_STATE使用权限。让我们看到完整的代码。



    <使用-sdk
        安卓:m​​inSdkVersion =“8”
        机器人:targetSdkVersion =“17”/>

   
     
    <应用
        机器人:allowBackup =“真”
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <活动
            android:name="com.example.callertalker.MainActivity"
            android:label="@string/app_name">
            
                

                
            
        
    


输出:


在实际设备(例如手机)上运行,用另一部手机拨打电话,您将听到来电号码。

相关文章