Blog / 阅读

android开发之选择联系人并返回电话号码

by admin on 2014-03-23 10:33:33 in ,



在跟着这个教程联系的时候,它所用到的选择联系人方式是自己从数据库里把联系人读取出来,然后用listview显示,选择后返回手机号码,


这样做在点击选择联系人的时候,需要时间去加载,并且显示出来的联系人好像并不全,


总之感觉不好,想通过调用系统联系人的方式选择,不用自己去处理界面。


结果还不错:


MainActivity:


[java] view plaincopyprint?
package jason.pickcontact;  
  
import android.app.Activity;  
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.TextView;  
  
public class MainActivity extends Activity {  
    Button pick;  
    TextView show;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        pick = (Button) findViewById(R.id.pick);  
        show = (TextView) findViewById(R.id.show);  
        pick.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                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);  
                show.setText("所选手机号为:" + num);  
            }  
            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;  
//                  switch (phone_type) {//此处请看下方注释  
//                  case 2:  
//                      result = phoneNumber;  
//                      break;  
//  
//                  default:  
//                      break;  
//                  }  
                }  
                if (!phone.isClosed()) {  
                    phone.close();  
                }  
            }  
        }  
        return result;  
    }  
  
    @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;  
    }  
  
}  
关于phone_type有些问题,联系人中把手机号存储的类型不同的话,phone_type也不同,一般来说手机那一栏的类型是2,


但是用2发现有的选不出来,去联系人查看才发现,好多联系人我并没有把号码存到手机这一栏,而是存在电话或者存为了其他类型,


把各种类型都存了电话测试发现,我手机是miui系统,手机那一栏的TYPE是2,住宅的TYPE是3和1,总机的TYPE是12和7,其他是7


在模拟器上用原生系统,发现mobile是2,home是1,work是3,other是7


不同系统有些差别,毕竟都有过改动,不过手机是2应该是没问题的,由于我存储的类型不对,所以我把代码中正确的注释掉了,没有判断类型就使用了。


标准来说的话,加一个类型2判断是对的。


关于联系人的数据库中的复杂关系没搞懂。先放着吧。


activity_main.xml:


[html] view plaincopyprint?
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <TextView  
        android:id="@+id/show"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world" />  
  
    <Button  
        android:id="@+id/pick"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/show"  
        android:layout_below="@+id/show"  
        android:text="选择联系人" />  
  
</RelativeLayout>  

转载信息:
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539


写评论

相关文章

上一篇:Android开发之android_apk 在线安装(源代码分享)

下一篇:android错误之OnTouchListener只能监听到ACTION_DOWN-----onTouchListener的返回值问题

评论

写评论

* 必填.

分享

栏目

赞助商


热门文章

Tag 云