title: Get Mobile Number From Contacts and Block that number in android
body: <p>i want to get a number from user or select a number from contact list and block that number. I am able to get contacts from my contact list with check box, now i want when i click on number and press Block Number the number will be block. I have tried all these links </p>
  
  <p>I am able to block a number define in the code. Now i want to select contact from my contact list and press block number the number will block</p>
  
  <pre><code>public class BlockCallReceiver &lt;MyAdapter&gt; extends  BroadcastReceiver{
  
      Context context;
      protected static final String TAG = null;
      public String[] Contacts = {};
      public int[] to = {};
      public ListView myListView;
      ArrayList&lt;String&gt; name1 = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; phno1 = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; phno0 = new ArrayList&lt;String&gt;();
      protected String toNumbers;
  
  
      Cursor mCursor = getContacts();
      startManagingCursor(mCursor);
      ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor,
              Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME },
              to = new int[] { android.R.id.text1 });
  
      setListAdapter(adapter);
      myListView = getListView();
      myListView.setItemsCanFocus(false);
      myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  
  
       clear_Button.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Toast.makeText(getApplicationContext(),"Selection Cleared", Toast.LENGTH_SHORT).show();
              ClearSelections();
          }
      });
  
      *//** When 'Done' Button Pushed: **//*
  
      done_Button.setOnClickListener(new View.OnClickListener() {
  
          public void onClick (View v){
  
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage("03234371984", null, "What's Up!!" , null, null);
  
              String name = null;
              String number = null;
              long [] ids = myListView.getCheckedItemIds();
              for(long id : ids) {
                  Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
                  while(contact.moveToNext()){
                      name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                      //name+=name;
                      number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      //number+=number;
                  }
                  Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
              }
          }
      });
  }
  
  
  private void ClearSelections() {
  int count = this.myListView.getAdapter().getCount();
  for (int i = 0; i &lt; count; i++) {
      this.myListView.setItemChecked(i, false);
  }
       }
  
      @SuppressWarnings("deprecation")
      private Cursor getContacts() {
  // Run query
  Uri uri = ContactsContract.Contacts.CONTENT_URI;
  String[] projection = new String[] { ContactsContract.Contacts._ID,
                                  ContactsContract.Contacts.DISPLAY_NAME};
  String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '"
          + ("1") + "'";
  String[] selectionArgs = null;
  String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
          + " COLLATE LOCALIZED ASC";
  
  return managedQuery(uri, projection, selection, selectionArgs,
          sortOrder);
  
   }
  
   private Cursor managedQuery(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
  // TODO Auto-generated method stub
  return null;
   }
  
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
  
       // TODO Auto-generated method stub
  
       Bundle myBundle = intent.getExtras();
       if (myBundle != null)
        {
           System.out.println("--------Not null-----");
           try 
           {
            if (intent.getAction().equals("android.intent.action.PHONE_STATE")) 
            { 
                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                System.out.println("--------in state-----");
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                { 
                    // Incoming call
                  String incomingNumber    =intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                  System.out.println("03335222339"+incomingNumber);
                  System.out.println("03234371984"+incomingNumber);
                  System.out.println("03244211843"+incomingNumber);
                  System.out.println("03006041071"+incomingNumber);
                  System.out.println("03056603902"+incomingNumber);
                  System.out.println("03224444774"+incomingNumber);
  
                  // this is main section of the code,. could also be use for particular number.
                  // Get the boring old TelephonyManager.
                        TelephonyManager telephonyManager =(TelephonyManager)   context.getSystemService(Context.TELEPHONY_SERVICE);
  
                        // Get the getITelephony() method                          
  
                        Class&lt;?&gt; classTelephony = Class.forName(telephonyManager.getClass().getName());
  
                        Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
  
                        // Ignore that the method is supposed to be private                          
                        methodGetITelephony.setAccessible(true);
  
                        // Invoke getITelephony() to get the ITelephony interface
                        Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
  
                        // Get the endCall method from ITelephony
                        Class&lt;?&gt; telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
                        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
  
                        // Invoke endCall()
                        methodEndCall.invoke(telephonyInterface);
  
                }
  
              }
             }
            catch (Exception ex) 
            { // Many things can go wrong with reflection calls
                ex.printStackTrace();
            }
           }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><a href="http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html" rel="nofollow noreferrer">Block Calls  programmatically</a> </p>
  
  <p><a href="http://mfarhan133.wordpress.com/2010/10/15/manipulating-incoming-ougoing-calls-tutorial-for-android/" rel="nofollow noreferrer">Block Calls  programmatically</a></p>
  
  <p><img src="https://i.stack.imgur.com/S3SXD.png" alt="When user select a number and press Block button the number will be block"></p>
  
  <p>When user select a number and press Block Number button the number will be block</p>