2

I'm new to Android. I'm developing an app which blocks unwanted calls. Now I'm stuck here when I try to compare the incoming number with the numbers in contact. Here is the code. Please help. Here while checking a Checkbox I need to block all calls from strangers (not in the contact)

Bundle extra=intent.getExtras();//new
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(extra!=null)//new=== getting the blocked number {
    state=extra.getString(telephonyManager.EXTRA_STATE);
    if(state.equals(telephonyManager.EXTRA_STATE_RINGING)) {
        number=extra.getString(telephonyManager.EXTRA_INCOMING_NUMBER);
        Log.w("INCOMMING NUMBER",number);
    }
}
if(noStrangers_cb.isChecked()){
    Cursor phones1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones1.moveToNext()){
        String phoneNumber = phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Log.d("NUMBER IN CONTACT",phoneNumber );

        //boolean val= number== phoneNumber;
        //String no=number;
        //Log.d("ASSIGNING NUMBER TO NO = ", no);
        //Collator c=Collator.getInstance();
        if(!phoneNumber.equals(number))
            {
            Log.d("IF ", "STRANGERS"+number);
            //Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
            try {
                telephonyService.endCall();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
    }
    phones1.close();
}//end if Strangers

LOGCAT: enter image description here

Updated code is below:

if(allContacts_cb.isChecked())
                     {
                      Log.d("BLOCK ALL CONTACTS","blocking contacts.........." );
                      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                       while (phones.moveToNext())
                       {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.d("NUMBER IN CONTACT ",phoneNumber );
                        String num=phoneNumber.replace("-","");
                        String incom=incomingNumber;
                        String s1="0"+num;
                        String s2="+91"+num;
if((num.equals(incomingNumber))||((s1).equals(incomingNumber))||(s2).equals(incomingNumber))//if(phoneNumber.compareTo(no)==0)//&&(checking==true))
                        { 
                          Log.d("IF ", "INSIDE IF OF BLOCKING CONTACTS");//+cn+"num"+n);
                          Log.d("NUMBER CHECKING", "NUMBER = "+number+"CONTACT = "+phoneNumber);
                          try {
                            ending1=telephonyService.endCall();
                            if(ending1)
                             {  
                             for(int i=1;i<=1;i++)
                             {
                              android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
                              sms.sendTextMessage(incom, null, SMS, null, null);
                              break;
                             }
                              }
                              } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                }

                          } 
                        else
                        {
                            if(noStrangers_cb.isChecked())
                            {
                                try {
                                    telephonyService.endCall();
                                } catch (RemoteException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                          }//end of while
                          phones.close();
                         }//end if allContacts

Can anyone help me.. Please..

Deepthi
  • 863
  • 1
  • 12
  • 20

3 Answers3

2

You can replace the string "-" with "" in your phone number and then use that string to compare.

string no= Phonecontactno.StringReplace("-","");

Now use this string to compare with incoming no.

subbu
  • 146
  • 1
  • 8
  • Thanks a lot.. I'll try this.. :-) – Deepthi May 25 '12 at 07:11
  • but here she doesn't want to replace or change the string, her question is just compare two string. – user1208720 May 25 '12 at 07:14
  • @subbu : Thanks a lot.. It solved one part of my problem.. But it doesn't work for checking inequality..That is for '!no.equals(incommingno)' Can you help me for that.. I'm upvoting this answers as it helped me to some extend.. – Deepthi May 25 '12 at 07:25
  • what is no and incommingno.Give some examples.My thought is no is Contact no taken from phone and incommingno is that incoming call no.I am right?If so r u comparing the incoming call no to contact no got from phone? – subbu May 25 '12 at 07:31
  • You are right.. no is number in contact and incommingno is the incomming number.. Yes you got me right.. I'm comparing them. – Deepthi May 25 '12 at 07:39
  • did u Know the two nos(no and incoming nos) when a call is coming.Suppose a number 12345 is coming as incoming call ,at that time what is the number in contact.Can u give some output.If u know that two nos b4 comparing, then u can come to an conclusion ,what's wrong with the two nos.. – subbu May 25 '12 at 07:47
  • I'm checking each number in contact with the incomming number ,ie, if 1234 is the incomming number and I've three numbers in contact, then 1234 will be checked with each of the number in contact. – Deepthi May 25 '12 at 08:49
  • if 1555521556 is incoming no and 1-555-521-556 is number in contact .Then after performing stringreplace function these 2 strings will match,now i think u got the desired result.What problem in this..Whether ur scenario is different?? – subbu May 25 '12 at 08:50
  • @Subbu : Thanks a lot.. after a long time.. Got my problem solved.. I just created a new avd and installed the app there and it now works like charm!! Android sometimes behaves strange. :) Thanks again.. :) – Deepthi May 29 '12 at 05:20
2

Replacing _ character from your number is not your final solution. You should read PhoneNumberUtils, or here is best library 'libphonenumber' which can be used for various type of operations related to phone numbers.

Read details about libphonenumber.

And below is code snipped which can be used for your purpose.

public boolean isCallerInDatabase(String caller, String phoneNumberFromDb) {
        phoneUtil = PhoneNumberUtil.getInstance();
        if(phoneUtil.isNumberMatch(caller, phoneNumberFromDb) != PhoneNumberUtil.MatchType.NO_MATCH) {
            //Number matched 
            Log.i("TEST", "NUMBER MATCHED");
            return true;
        }
        Log.i("TEST", "NUMBER NOT MATCHED");
        return false;       
    }

Update: Android Added that library as Utility class.

Android itself has been added PhoneNumberUtils class for phone related helper methods. To compare two Phone Numbers you should use PhoneNumberUtils.compare method.

Happy coding :)

Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
  • @Deepthi It works. I used in my application which runs on 2.1 to 4.2. So I am sure it works for 2.2. – Pankaj Kumar Jun 05 '12 at 05:09
  • I've updated my question.. Please check the code which is at the botton of my question..Thanks in advance – Deepthi Jun 05 '12 at 05:25
  • @Deepthi Hey I Saw your code. Where did you implemented libphonenumber ? I have some guidelines for you... There is no need to add '0' or '+91' to numbers while checking..... and no need to replace '_' character from number. .. when you use libphonenumber everything will take care by this library. – Pankaj Kumar Jun 05 '12 at 05:59
  • I'm new to android so could you please tell me how to include libphonenumber in my code..Just give me an example code. – Deepthi Jun 05 '12 at 06:03
  • @Deepthi Read http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project to know about how to add lib in Android project. And when you will add lib, simply use my code where you have to pass incoming number and number from database. Hope this will help you. – Pankaj Kumar Jun 05 '12 at 06:18
  • When I try your code, it shows error saying to change to PhoneNumberUtils and when I do that there is no getinstance() method. What is the issue? – Deepthi Jun 05 '12 at 06:19
  • hi Pankanj.. I added the lib file and did everything that is said in that answer of the SO question you posted but still the same.. – Deepthi Jun 05 '12 at 07:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12148/discussion-between-pankaj-kumar-and-deepthi) – Pankaj Kumar Jun 05 '12 at 07:04
0

try the following condition may be it helps you

if(!(phoneNumber.toString().equals(number.toString)))
user1208720
  • 532
  • 2
  • 6
  • ya, but here first read your question , you ask about comparing two strings, not to replace it, and then compare it,:( – user1208720 May 25 '12 at 07:31
  • I'm unable to compare incomming number with the number in contact.. :( – Deepthi May 25 '12 at 07:41
  • here As per your log, i can see your stored contact number is merged with dash(-) , so either your stored contact number are wrong or if they are in write format then the incoming number never match with the stored Contact number, but yes if as per your requirement if they are merged with dash then you can replace dash("-") with ("") as per subbu said. – user1208720 May 25 '12 at 07:49