查询电话联系人(包括sim卡)

来源:互联网 发布:淘宝账号注册自动 编辑:程序博客网 时间:2024/06/02 13:41

今天遇到一个需求查找联系人,并选择后电话号码于是上网搜索查找相关资料,但是网上的我搜到的例子都是能够查询到系统联系人却不能

查到sim卡里的,所以做了个案例记录。



/**

 * 转账界面
 * @author haie
 *
 */
public class TransferActivity extends Activity{

    @ViewInject(id=R.id.ed_phone)EditText edphone;
    @ViewInject(id=R.id.ed_money)EditText edmoney;
    @ViewInject(id=R.id.ib_phones)Button btSure;
    private String phoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_transfer);
        
        
    }

    public void getPhoneNum(View v){
        /*Intent i = new Intent(Intent.ACTION_PICK);
        i.setType("content://icc/and");//sim卡联系人      //vnd.android.cursor.dir/contact(手机联系人)
        startActivityForResult(i, 0);*/
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  //
        startActivityForResult(intent, 0);  
    }
    
     protected void onActivityResult (int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            
            switch (requestCode) {
                case 0:
                    if (data == null) {
                        return;
                    }
                    //localCursor1是为了获得contact_id
                    ContentResolver localContentResolver = getContentResolver();
                    Cursor localCursor1 = managedQuery(data.getData(), null, null, null, null);
                    
                    if(localCursor1.moveToFirst()){
                        String str = localCursor1.getString(localCursor1.getColumnIndex("_id"));
                        //根据contact_id查询电话号码
                        Cursor localCursor2 = localContentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = " + str, null, null);
                        while (localCursor2.moveToNext())
                        {
                          this.phoneNumber = localCursor2.getString(localCursor2.getColumnIndex("data1"));
                        }
                      //  continue;
                        if (resultCode == -1)
                        {
                          setResult(resultCode, data);
                          finish();
                        }
                    }
                    
                   // ToastUtils.showInfo(TransferActivity.this, phoneNumber);
                    edphone.setText(phoneNumber);
                    
                default:
                    break;
            }
        }

     /**
      * 跳转到确认转账界面
      */
     public void TransferConfirm(View v){
         String phone=edphone.getText().toString();
         String money=edmoney.getText().toString();
         if(CheckUtils.checkEmpty(phone)||phone.length()<11){
             ToastUtils.showInfo(TransferActivity.this, "手机号码输入错误");
             return;
         }else if(CheckUtils.checkEmpty(phone)){
             ToastUtils.showInfo(TransferActivity.this, "转账金额须大于零");
             return;
         }else if(phone.startsWith("0")){
             ToastUtils.showInfo(TransferActivity.this, "转账金额为非法数据");
             return;
         }
            Intent localIntent = new Intent(this, TransferConfirmActivity.class);
            Bundle localBundle = new Bundle();
            localBundle.putString("phone", phone);
            localBundle.putString("amount", money);
            localIntent.putExtras(localBundle);
            startActivityForResult(localIntent, 2);
     }
    
     /**
         * 点击返回到搜索界面
         * @param v
         */
        public void btBack(View v){
            finish();
        }

        /**
         * 返回键事件
         */
        @Override  
        public boolean onKeyDown(int keyCode, KeyEvent event) {  
            if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){  
                //需要处理  
                this.finish();
            }  
            return false;  
        }
}

0 0
原创粉丝点击