0

HERE MY CODE .. For any incoming number i try to find the caller name and return the name where this method invoke .. pls help

public  String getName(String number){

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

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri,new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

    try{
        if(contactLookup != null && contactLookup.getCount()>0 ){
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
        }
    }
    finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}

Here the method invoke part

public void onCallStateChanged(int state, String incomingNumber) {
    String s = incomingNumber;
    String ss = getName(s);
    if(state== TelephonyManager.CALL_STATE_RINGING ){
        tts.speak(ss +" calling please answer ", TextToSpeech.QUEUE_FLUSH, null);
        Toast.makeText(getApplicationContext(), "Phone is Ringing : " + incomingNumber, Toast.LENGTH_LONG).show();
    }
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99

1 Answers1

0

Please use PhoneStateListener. It has an onCallStateChanged handler; one of the supplied arguments you'll get is a String containing the incoming phone number.

Check the question here for more details about implementation.

Community
  • 1
  • 1
g90
  • 506
  • 3
  • 18
  • Check again i also add where the method invoked @geek90 – jahidul hasan Dec 22 '15 at 20:39
  • Can you explain what issue you are facing ? – g90 Dec 22 '15 at 22:01
  • Post the complete stacktrace, looks like there is an issue with your getName method. – g90 Dec 23 '15 at 00:39
  • 12-23 06:41:59.727 6812-6812/com.example.jahid.firsttalk E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.jahid.firsttalk, PID: 6812 java.lang.IllegalArgumentException: URI: content://com.android.contacts/phone_lookup/, calling user: com.example.jahid.firsttalk, calling package:com.example.jahid.firsttalk at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137) – jahidul hasan Dec 23 '15 at 11:49
  • at android.content.ContentResolver.query(ContentResolver.java:478) at android.content.ContentResolver.query(ContentResolver.java:422) at com.example.jahid.firsttalk.MainActivity.getName(MainActivity.java:89) at com.example.jahid.firsttalk.MainActivity$1.onCallStateChanged(MainActivity.java:39) at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:293) at android.os.Handler.dispatchMessage(Handler.java:102) – jahidul hasan Dec 23 '15 at 11:50
  • at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) – jahidul hasan Dec 23 '15 at 11:50
  • i think problem may be there ------ Cursor contactLookup = contentResolver.query(uri,new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); Error :-com.example.jahid.firsttalk.MainActivity.getName(MainActivity.java:89) at – jahidul hasan Dec 23 '15 at 12:14