Android说来电者姓名

时间:2014-02-21 06:21:08

标签: android

如何在接听电话时说出来电者姓名。 https://groups.google.com/forum/#!topic/tasker/V_z1YJ6lBGQ http://www.tutorialspoint.com/android/android_text_to_speech.htm 有提到的东西,但我已经这样做了,我收到了来电者姓名,现在我想把这个文本转换成接收方法的语音。 我确实使用了事件处理程序,并将该名称发送给其他活动,但是当应用程序关闭时它没有说出名称。

2 个答案:

答案 0 :(得分:0)

Bundle bundle = intent.getExtras();


            if(null == bundle)
                    return;


           String state = bundle.getString(TelephonyManager.EXTRA_STATE);


            if(incoming_flag==true){
            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
            {

                    ////////testing ends

                    String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                  //  String personname=bundle.getString(TelephonyManager.)       
                    Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
                    ContentResolver con =null;


                    //testing name
                    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumber));

                    Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
                    if (cursor.moveToFirst())
                    {
                         name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                    }   

你必须为它生成一个广播以检测来电,使用onRecieve param的意图获取名称上面的示例代码显示了这样做的方式,你将不得不编写一个服务,它将从广播到播放来电者姓名使用文字转语音方法

答案 1 :(得分:0)

首次检测到任何新呼叫到达时的联系号码,此线程将帮助您这样做

Retrieve incoming call's phone number in Android

一旦您检测到来电的联系电话,您就可以将该联系电话号码与设备上保存的所有联系人列表进行比较。

使用以下方法获取设备中的所有联系人姓名和号码

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(
                               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                               null,
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                               new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                     }
                    pCur.close();
                }
            }
        }

有关此内容的详细信息,请查看本指南

http://saigeethamn.blogspot.in/2011/05/contacts-api-20-and-above-android.html

相关问题