Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:52:58+00:00 2026-06-05T00:52:58+00:00

I’m new to Android. I’m developing an app which blocks unwanted calls. Now I’m

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:53:01+00:00Added an answer on June 5, 2026 at 12:53 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.