从联系人中获取手机号码并在Android中阻止该号码

时间:2014-07-18 13:41:48

标签: android android-layout

我想从用户那里获取一个号码或从联系人列表中选择一个号码并阻止该号码。我可以通过复选框从我的联系人列表中获取联系人,现在我想要当我点击号码并按下块编号时,该号码将被阻止。我已经尝试了所有这些链接

我能够阻止代码中的数字定义。现在我想从我的联系人列表中选择联系人,然后按块编号阻止该号码

public class BlockCallReceiver <MyAdapter> extends  BroadcastReceiver{

    Context context;
    protected static final String TAG = null;
    public String[] Contacts = {};
    public int[] to = {};
    public ListView myListView;
    ArrayList<String> name1 = new ArrayList<String>();
    ArrayList<String> phno1 = new ArrayList<String>();
    ArrayList<String> phno0 = new ArrayList<String>();
    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 < 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<?> 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<?> 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();
          }
         }
}

}

Block Calls programmatically

Block Calls programmatically

When user select a number and press Block button the number will be block

当用户选择一个号码并按下Block Number按钮时,该号码将被阻止

0 个答案:

没有答案