-3

The result in the following always return true even if the telphone number listed in contact isn't starred, why?

public static boolean IsStarred(Context myContext,String address){
    boolean result=false;
    if (address.trim().length() >= 1) {
        Uri uri = Uri.parse("content://com.android.contacts/phone_lookup");
        String[] projection = new String[] { "display_name" };
        uri = Uri.withAppendedPath(uri, Uri.encode(address));
        Cursor cursor = myContext.getContentResolver().query(uri,
                projection, "starred=?", new String[] {"1"}, null);
        if (cursor.moveToFirst()) {
            result = true;
            Toast.makeText(myContext,cursor.getString(0)+ "True",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(myContext, "False",Toast.LENGTH_SHORT).show();
        }
        cursor.close();
    }

    return result;
}
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

1

Providing full working code would require a very long answer, which is generally out of the scope of this site.

I would recommend you try to divide this requirement into its components. If you want to know if an incoming call is from a starred contact, that would imply:

  1. Detecting incoming calls, and determining the calling number. This can be achieved by combining permissions, a BroadcastReceiver, and a PhoneStateListener. An excellent answer by @GabeSechan can be found here.
  2. Finding a contact given its phone number. This requires querying a ContentProvider, in particular the PhoneLookup contract seems useful for this case.
  3. Determining whether this contact is starred (here it's easy, since the PhoneLookup table includes that column.

If you encounter specific problems implementing this solution, please post a more detailed question.

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154