ContentResolver 内容访问者访问手机联系人

内容访问者访问系统联系人,有两种方式,第一种是用Intent跳转系统自带的Activity,显示联系人信息。第二种是从系统数据库中得到联系人数据显示到Listview中。
第一种Intent跳转:

public class Check2Activity extends AppCompatActivity {

    private EditText check2_text;
    private PendingIntent pIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check2);
        //获取电话号码
        check2_text = (EditText) findViewById(R.id.check2_text);
        check2_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://contacts/people");
                Intent intent = new Intent(Intent.ACTION_PICK, uri);
                startActivityForResult(intent, 0);
            }
        });
    }
    /*
 * 跳转联系人列表的回调函数
 * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode){
            case 0:
                if(data==null)
                {
                    return;
                }
                //处理返回的data,获取选择的联系人信息
                Uri uri=data.getData();
                String[] contacts=getPhoneContacts(uri);
               // et_name.setText(contacts[0]);
                check2_text.setText(contacts[1]);
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    private String[] getPhoneContacts(Uri uri){
        String[] contact=new String[2];
        //得到ContentResolver对象
        ContentResolver cr = getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor=cr.query(uri,null,null,null,null);
        if(cursor!=null)
        {
            cursor.moveToFirst();
            //取得联系人姓名
            int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            contact[0]=cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            if(phone != null){
                phone.moveToFirst();
                contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phone.close();
            cursor.close();
        }
        else
        {
            return null;
        }
        return contact;
    }

    public void OK(View view){
        //得到输入的电话号码
        String phone=check2_text.getText().toString();
        new MyApplication().phone=check2_text.getText().toString();
        //发送内容(自定义)
        String content="请执行如下指令,对手机进行远程操作:\n" +
                "\t1.关机:#close#\n" +
                "\t2.音乐:#ring#\n"+
           "\t3.重启:#restart#";
        //得到信息管理者(默认的)
        Intent intent=new Intent();
        intent.putExtra("data",content);
        pIntent = PendingIntent.getBroadcast(this,0,intent,0);
        SmsManager manager = SmsManager.getDefault();
        ArrayList list = manager.divideMessage(content);
        //因为一条短信有字数限制,因此要将长短信拆分
        for(String text:list){
            manager.sendTextMessage(phone, null, text, null, null);
            Log.i("tt","发送成功了!!!!!!"+ new MyApplication().phone+"    "+text);
        }
        finish();
        Intent intent2 = new Intent();
        intent2.setClass(Check2Activity.this, MainActivity.class);
        startActivity(intent2);
    }

第二种用内容访问者访问系统数据库得到联系人数据:

public class ContactsActivity extends AppCompatActivity {

    private ContentResolver cr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        //得到内容访问者
        cr = getContentResolver();
        /**
         * <1>content://com.android.contacts/raw_contacts(获得联系人的id,姓名)
         <2>content://com.android.contacts/raw_contacts/"+id+"/data(根据联系人的id获得联系人的信息)
         */
    }
    public void getContacts(View view){
        //获取联系人的ID及名字
        Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cursor=cr.query(uri,null,null,null,null);

        while(cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("display_name"));
            Log.i("tt","联系人id:"+id+" 联系人名字:"+name);

            //根据联系人的ID获取联系人相对应的数据(名字+邮箱+phone)
            Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cursorData=cr.query(uriData,null,null,null,null);

            while(cursorData.moveToNext()){
                String data1=cursorData.getString(cursorData.getColumnIndex("data1"));
                //int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
                String type=cursorData.getString(cursorData.getColumnIndex("mimetype"));
                if("vnd.android.cursor.item/phone_v2".equals(type)){
                    Log.i("tt","   联系人号码:"+data1+"    联系人类型:"+type);
                }
            }
        }
    }

}

然后再将其显示到自己写的listview中。

你可能感兴趣的:(Android,综合)