2012-08-29 59 views
0

在Android中撥打電話時,我無法顯示聯繫人姓名。如何以編程方式在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); 
+0

做這與你的意圖是如何工作的認識的一個根本問題。 – JoxTraex

+0

我是新來的android – AndroidRaji

回答

0

對於您需要創建一個廣播接收器和的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; 
    } 


} 

代碼下載代碼訪問這個link由維諾德Dirishala

相關問題