1

在 Android 中拨打电话时无法显示联系人姓名。

这是我的代码:

String callTo = "9999900000";
String callername="xxx";    
Intent callIntent = new Intent(Intent.ACTION_CALL);                 
callIntent.setData(Uri.parse("tel:"+ callTo));                  
String srvname=Context.TELEPHONY_SERVICE;    
TelephonyManager tm=(TelephonyManager)getSystemService(srvname);                     
startActivity(callIntent);
4

1 回答 1

1

为此,您需要创建一个广播接收器并在OnReceive方法中检查一个OUTGOING CALL状态,然后从中获取呼叫者号码,然后创建另一个方法来检查您的手机中是否存在联系人,以便在该方法中传递该联系人号码。

下面是来电者姓名广播接收器的代码

package com.example.user.caller;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;


import android.telephony.TelephonyManager;
import android.widget.Toast;



/**
 * Created by Vinod Dirishala on 11/27/2016.
 */
public class MyReceiver extends BroadcastReceiver {

    String phoneNumber;
    String name;



    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        Bundle extras=intent.getExtras();
        if(action.equals("android.intent.action.PHONE_STATE")){
            String state=extras.getString(TelephonyManager.EXTRA_STATE);
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){

                String incomingnumber=extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                String msg = "";
                //  String person = getCname(context,msg);

                String names = getContactDisplayNameByNumber(incomingnumber,context);

                //  Toast.makeText(context,"Caller Name"+person,Toast.LENGTH_SHORT).show();

                displayToast(context,"phone is ringing"+incomingnumber+ "calling you mr."+names);

                Intent intent1  = new Intent(context,
                        MainActivity.class);
                intent.putExtra("contactname", names);  //<<< put sms text
                context.startActivity(intent);
                //  displayToast(context,"phone is ringing"+person);
                // displayToast(context,"Vinod your  phone is ringing"+contacname);
            }else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                Toast.makeText(context,"phone in idle",Toast.LENGTH_SHORT).show();
            }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                Toast.makeText(context,"phone in offhook",Toast.LENGTH_SHORT).show();
            }

        }else if(action.equals("android.provider.Telephony.SMS_RECEIVED")){

            displayToast(context,"SMS Recived");
        }
    }




    private void displayToast(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
    public String getCname(Context context,String name)
    {
        ContentResolver cr=context.getContentResolver();
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println(" Phone Nuber.................."+phoneNumber);
            //  aa.add(name);
            // aa.add(phoneNumber);

        }
        return name;
    }

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

        ContentResolver contentResolver = context.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));
            }else{
                name = "Unknown number";
            }
        } finally {
            if (contactLookup != null) {
                contactLookup.close();
            }
        }

        return name;
    }


}

要下载代码,请访问Vinod Dirishala 完成的此链接

于 2016-12-18T14:19:00.350 回答