English 中文(简体)
在按钮点击事件中打开设备联系人列表
原标题:Open device contacts list at button click event

如何在按钮点击事件中打开Android设备联系人列表。

问题回答

试试这个代码。。

    yourButton.setOnClickListener(new YouButtonEvent());


    class YouButtonEventimplements OnClickListener{

    @Override
    public void onClick(View v) {

        Intent it= new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);

        startActivityForResult(it, PICK_CONTACT);

    }

}

声明一些变量。创建一个方法&;处理事件。

private static final int CONTACT_PICKER_RESULT = 1001;
private static final String DEBUG_TAG = "Contact List";
private static final int RESULT_OK = -1;

// a method to open your contact list
private void openContactList() {

    Intent it = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(it, CONTACT_PICKER_RESULT);

}

// handle after selecting a contact from the list
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            // handle contact results
            Log.w(DEBUG_TAG, "Warning: activity result is ok!");
            break;
        }
    } else {
        // gracefully handle failure
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

您可以使用以下源代码作为参考:

import android.app.Activity; 
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Test1Activity extends Activity  {

    private static final int PICK_CONTACT_REQUEST = 1;

    private static final int PICK_CONTACT = 0;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button pickContact = (Button) findViewById(R.id.button1);

        pickContact.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) {

                Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);   

                startActivity(i);
            }
        });

    }
}

如果你想从你的设备中挑选联系人,请使用此代码。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           openContect();
            dialog.dismiss();
        }

openContact()是:

 private void openContect() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_CONTACT);
    }
}

并且在onActivityResult()中使用以下内容:

if (requestCode==REQUEST_SELECT_CONTACT && resultCode == RESULT_OK && null != data){
        Uri contactUri = data.getData();
       //do what you want...
    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签