在android中使用语音呼叫app

时间:2018-04-06 03:27:25

标签: android text-to-speech

我正在尝试创建一个使用Text-To-Speech进行调用的应用程序。我试图让应用程序从用户说的联系人名称中获取数字。例如:致电萨拉。我希望应用程序能够获得Sarah的号码并拨打电话。没有显示错误,我不确定它为什么不起作用。请帮助。

private void call(String contact) {

    String[] PROJECTION = new String[]{
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };
    ContentResolver cr = getContentResolver();
    // Execute the query and receive the cursor with the results
    Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, new String[]{contact},
            null);
    final int indexNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    String number = cursor.getString(indexNumber);
    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
        getPermissionToCall();
    }
    else{
        startActivity(callIntent);
    }
}

要求许可

 public void getPermissionToCall() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        if(shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)){
            Toast.makeText(this, "Requires permission to access call function", Toast.LENGTH_SHORT).show();
        }
    requestPermissions(new String[]{Manifest.permission.CALL_PHONE},READ_CALL_PERMISSIONS_REQUEST);
    }
}

识别方法

 private void recognition(String text) {
    Log.e("Speech", "" + text);
    //creating an array which contains the words of the answer
    String[] speech = text.split(" ");
if(text.startsWith("Call")){
         String caller = speech[speech.length - 1];
        call(caller);

0 个答案:

没有答案
相关问题