首页 > 其他 > 详细

调用contact某个联系人资料后返回数据到原来的Activity

时间:2014-08-01 22:54:52      阅读:525      评论:0      收藏:0      [点我收藏+]
package com.example.sample_5_10;

import android.app.Activity;
import android.app.Instrumentation.ActivityResult;
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.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    EditText et1;
    EditText et2;
    Button bt;
    private static final int REQUESTCODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.editText1);
        et2 = (EditText) findViewById(R.id.editText2);
        bt = (Button) findViewById(R.id.button1);
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivityForResult(
                        new Intent(
                                Intent.ACTION_PICK,
                                android.provider.ContactsContract.Contacts.CONTENT_URI),
                        REQUESTCODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        switch (requestCode) {
        case REQUESTCODE:
            final Uri uriret = data.getData();
            if (uriret != null) {

                try {
                    CursorLoader loader = new CursorLoader(MainActivity.this,
                            uriret, null, null, null, null);
                    Cursor c = loader.loadInBackground();
                    c.moveToFirst();
                    String strname = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    et1.setText(strname);
                    int contact_id = c.getInt(c
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + "=" + contact_id, null, null);
                    if (phones.getCount() > 0) {
                        phones.moveToFirst();
                        String phonename = phones
                                .getString(phones
                                        .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        et2.setText(phonename);
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
            break;

        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

联系人电话号码默认选首个,需要全部显示可以用for循环phones游标即可。

调用contact某个联系人资料后返回数据到原来的Activity,布布扣,bubuko.com

调用contact某个联系人资料后返回数据到原来的Activity

原文:http://www.cnblogs.com/mf0819/p/3885774.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!