zoukankan      html  css  js  c++  java
  • Android ContentResolve使用

    在Android中使用ContentResolve访问其他程序的数据:

    http://developer.android.com/reference/android/content/ContentProvider.html

    使用ContentResolve访问手机的联系人

    MainActivity.java:注意getContentResolver.query方法的使用实际为解析一个URI

    package org.elvalad.contactstest;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class MainActivity extends Activity {
    
        ListView contactsView;
        ArrayAdapter<String> adapter;
        List<String> contactsList = new ArrayList<String>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            contactsView = (ListView) findViewById(R.id.contacts_view);
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList);
            contactsView.setAdapter(adapter);
            readContacts();
        }
    
        private void readContacts() {
            Cursor cursor = null;
            try {
                cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
                while (cursor.moveToNext()) {
                    // 获取联系人姓名
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    // 获取联系人手机号
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactsList.add(displayName + "
    " + number);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <ListView
            android:id="@+id/contacts_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    
    </LinearLayout>

    AndroidManifest.xml:增加读取联系人权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.elvalad.contactstest" >
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        
        <uses-permission android:name="android.permission.READ_CONTACTS" />
    
    </manifest>
  • 相关阅读:
    MYSQL索引使用
    事务的概念是什么,有哪些基本属性?
    springboot和springmvc的区别
    List、Map、Set的区别与联系
    MyBatis-动态SQL
    MyBatis-映射文件
    MyBatis操作数据库及全局配置文件
    Jmeter的基本使用
    MySQL索引优化
    MySQL索引
  • 原文地址:https://www.cnblogs.com/elvalad/p/4430438.html
Copyright © 2011-2022 走看看