Android获取电话号码实例

jopen 5年前

在Androidmanifest注册获取权限<uses-permission android:name="android.permission.READ_CONTACTS"/>

源码如下:

    package com.example.intentphone;                import android.app.Activity;        import android.content.CursorLoader;        import android.content.Intent;        import android.database.Cursor;        import android.net.Uri;        import android.os.Bundle;        import android.provider.ContactsContract;        import android.view.Menu;        import android.view.View;        import android.view.View.OnClickListener;        import android.widget.Button;        import android.widget.EditText;                public class MainActivity extends Activity {            EditText ed1;            EditText ed2;            Button bt1;                    @Override            protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.activity_main);                ed1 = (EditText) findViewById(R.id.ed1);                ed2 = (EditText) findViewById(R.id.ed2);                bt1 = (Button) findViewById(R.id.bt1);                // 启动系统应用程序组件                bt1.setOnClickListener(new OnClickListener() {                            @Override                    public void onClick(View v) {                        // TODO Auto-generated method stub                        // 创建Intent对象                        Intent intent = new Intent();                        // 设置Intent的Action属性                        intent.setAction(Intent.ACTION_GET_CONTENT);                        // 显示指定Intent的数据类型                        intent.setType("vnd.android.cursor.item/phone");                        // 启动系统Activity,请求码为零                        startActivityForResult(intent, 0);                    }                });                    }                    @Override            protected void onActivityResult(int requestCode, int resultCode, Intent data) {                // TODO Auto-generated method stub                super.onActivityResult(requestCode, resultCode, data);                if (requestCode == 0) {                    if (resultCode == Activity.RESULT_OK) {                        // 获取返回的数据                        Uri contactData = data.getData();                        CursorLoader cursorLoader = new CursorLoader(this, contactData,                                null, null, null, null);                        // 查询联系人信息                        Cursor cursor = cursorLoader.loadInBackground();                        // 如果查询到指定的联系人                        if (cursor.moveToFirst()) {                            String contactId = cursor.getString(cursor                                    .getColumnIndex(ContactsContract.Contacts._ID));                            // 获取指定的联系人查询该联系人的信息                            String name = cursor                                    .getString(cursor                                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));                            String phoneNumbere = "此联系人暂时未输入电话号码";                            Cursor phones = getContentResolver().query(                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                                    null,                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID                                            + "=" + contactId, null, null);                            if (phones.moveToFirst()) {                                // 取出电话号码                                phoneNumbere = phones                                        .getString(phones                                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                            }                            //关闭游标                            phones.close();                            //显示联系人与联系电话                            ed1.setText(name);                            ed2.setText(phoneNumbere);                                }                            }                }            }                    @Override            public boolean onCreateOptionsMenu(Menu menu) {                // Inflate the menu; this adds items to the action bar if it is present.                getMenuInflater().inflate(R.menu.main, menu);                return true;            }                }  
</div> </div>
<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:orientation="vertical"        tools:context=".MainActivity" >                <EditText            android:id="@+id/ed1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:cursorVisible="false"            android:editable="false" />            <EditText            android:id="@+id/ed2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:cursorVisible="false"            android:editable="false" />            <Button            android:id="@+id/bt1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="查看联系人" />            </LinearLayout>  
</div> </div> 来自:http://blog.csdn.net/x605940745/article/details/15339231