当手机响铃时,我该如何说出来电者的姓名?

时间:2014-03-21 07:42:10

标签: android

当手机响铃时,正在播放铃声,我想通过铃声说出来电者的姓名,我该怎么办?有一些示例代码吗?谢谢!

此外,我可以通过编程方式拒绝电话号码吗?有一些示例代码吗?

1 个答案:

答案 0 :(得分:5)

这可以通过实现广播接收器并侦听TelephonyManager.ACTION_PHONE_STATE_CHANGED操作来实现。

第1步: 为来电创建广播接收器类

package com.javaorigin.android.sample;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallReceiver extends BroadcastReceiver {
 Context context = null;
 private static final String TAG = "Phone call";
 private ITelephony telephonyService;

 @Override
public void onReceive(Context context, Intent intent) {                                         // 2
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                         // 3
    String msg = "Phone state changed to " + state;

    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {                                   // 4
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);  // 5
        msg += ". Incoming number is " + incomingNumber;

        // TODO This would be a good place to "Do something when the phone rings" ;-)

    }

    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

}

}

第2步: 创建IDL接口以获取核心Telephony服务 包名必须是com.android.internal.telephony

FileName:ITelephony.aidl   包com.android.internal.telephony;

界面ITelephony {

boolean endCall();


void answerRingingCall();


void silenceRinger();

}

第3步: AndroidManifest.xml配置   
    

    <receiver  android:name=".PhoneCallReceiver">
        <intent-filter  android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-sdk android:minSdkVersion="8" />

如果您的联系人列表中保存了联系人号,请尝试使用以下方法获取名称。只需将文本中的返回值“name”传递给语音,然后您就可以听到来电者的姓名。

public String getContactDisplayNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String name = "Incoming call from";

    ContentResolver contentResolver = c.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, null, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            // this.id =
            // contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID));
            // String contactId =
            // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}