码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-01 22:54:52      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   os   io   数据   for   

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

标签:android   style   blog   color   os   io   数据   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!