国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ContentResolver(獲取系統聯系人)<手動展示;調用系統界面>

2019-11-09 15:26:26
字體:
來源:轉載
供稿:網友
public class MainActivity extends AppCompatActivity {    PRivate ContentResolver contentResolver;    private ListView contacts_list;    private Cursor cursor1;    private List<Map<String,String>> list=new ArrayList<>();    private SimpleAdapter simpleAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化 控件        initview();    }    //初始化 控件    private void initview(){        //獲取內容 訪問者        contentResolver = getContentResolver();        contacts_list = (ListView) findViewById(R.id.contacts_list);        //獲得所有 聯系人        findViewById(R.id.getAllContacts_btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");                Cursor cursor=contentResolver.query(uri,null,null,null,null);                while (cursor.moveToNext()){                   //獲得所有聯系人的 id                   int id=cursor.getInt(cursor.getColumnIndex("_id"));                   String name=cursor.getString(cursor.getColumnIndex("display_name"));                    //繼續獲取相對應的聯系人的數據(電話號碼)                    Uri uriAll=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");                    cursor1 = contentResolver.query(uriAll,null,null,null,null);                      //獲取聯系人 id  名字  電話                      while (cursor1.moveToNext()){                          String data1=cursor1.getString(cursor1.getColumnIndex("data1"));                          String type=cursor1.getString(cursor1.getColumnIndex("mimetype"));                          if("vnd.android.cursor.item/phone_v2".equals(type)){                              Map<String,String> map=new HashMap<String, String>();                              map.put("id",id+"");                              map.put("name",name);                              map.put("phone",data1);                              list.add(map);                          }                    }                    simpleAdapter = new SimpleAdapter(MainActivity.this,list, R.layout.contacts_item,new String[]{"id","name","phone"},new int[]{R.id.contacts_id_tv,R.id.contacts_name_tv,R.id.contacts_phone_tv});                    contacts_list.setAdapter(simpleAdapter);                }            }        });        //頁面跳轉 獲取指定聯系人        findViewById(R.id.ItetentJumpForSingleContacts_btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent(Intent.ACTION_PICK,                        ContactsContract.Contacts.CONTENT_URI);                MainActivity.this.startActivityForResult(intent, 1);            }        });    }       @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case 1:                if (resultCode == RESULT_OK) {                    Uri contactData = data.getData();                    Cursor cursor = managedQuery(contactData, null, null, null,                            null);                    cursor.moveToFirst();                    String num = this.getContactPhone(cursor);                    Toast.makeText(this, ""+num, Toast.LENGTH_SHORT).show();                }                break;            default:                break;        }    }    private String getContactPhone(Cursor cursor) {        // TODO Auto-generated method stub        int phoneColumn = cursor                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);        int phoneNum = cursor.getInt(phoneColumn);        String result = "";        if (phoneNum > 0) {            // 獲得聯系人的ID號            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);            String contactId = cursor.getString(idColumn);            // 獲得聯系人電話的cursor            Cursor phone = getContentResolver().query(                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                    null,                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="                            + contactId, null, null);            if (phone.moveToFirst()) {                for (; !phone.isAfterLast(); phone.moveToNext()) {                    int index = phone                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);                    int typeindex = phone                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);                    int phone_type = phone.getInt(typeindex);                    String phoneNumber = phone.getString(index);                    result = phoneNumber;                }                if (!phone.isClosed()) {                    phone.close();                }            }        }        return result;    }}

手動展示:

  首先獲取系統訪問者ContnetResolver,

   再通過Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");獲得手機中所有的聯系人Id,以及名字,

   再使用 Uri uriAll=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");根據id查詢該人的電話,

跳轉系統界面,吐司電話號碼:

  Intent intent = new Intent(Intent.ACTION_PICK,                        ContactsContract.Contacts.CONTENT_URI);                MainActivity.this.startActivityForResult(intent, 1);
 跳轉到系統的聯系人選擇界面,只能單選;
   通過回調方法傳回來的值,判斷有無選中;
   有選中獲得號碼,吐司; 
   無則無操作;


上一篇:Swift3.0--懶加載

下一篇:安裝apk的代碼

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河南省| 仁怀市| 前郭尔| 合川市| 翼城县| 阿拉善盟| 精河县| 赣榆县| 富平县| 东乡县| 达州市| 庆城县| 米脂县| 永泰县| 北宁市| 芮城县| 高阳县| 镇康县| 信丰县| 临漳县| 英山县| 大渡口区| 湘乡市| 墨玉县| 日照市| 青神县| 建湖县| 南阳市| 山阳县| 社会| 潜山县| 临澧县| 茌平县| 辰溪县| 自贡市| 淅川县| 罗平县| 兴城市| 安多县| 安远县| 正蓝旗|