java-如何从Android Studio中检索到的联系人列表中调用联系人?

java-如何从Android Studio中检索到的联系人列表中调用联系人?,第1张

概述此代码使我可以从用户的电话中检索联系人列表并显示它们.我正在做一些修改,我给每个联系人添加了一个“呼叫”按钮,但是我很难理解如何仅检索电话号码.取得电话号码后,我就可以拨打电话:IntentcallIntent=newIntent(Intent.ACTION_CALL);callIntent.setData(Uri.parse("03777

此代码使我可以从用户的电话中检索联系人列表并显示它们.我正在做一些修改,我给每个联系人添加了一个“呼叫”按钮,但是我很难理解如何仅检索电话号码.取得电话号码后,我就可以拨打电话:

Intent callintent = new Intent(Intent.ACTION_CALL);callintent.setData(Uri.parse("0377778888"));

我怎么知道该打哪个电话?我怎么知道x用户有y号,然后将该数字传递给意图?

这是代码:
Contacts.java

package edu.utep.cs.cs4330.easytech;import androID.app.Activity;import androID.app.ProgressDialog;import androID.content.ContentResolver;import androID.database.Cursor;import androID.net.Uri;import androID.os.Bundle;import androID.os.Handler;import androID.provIDer.ContactsContract;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.Toast;import java.util.ArrayList;public class Contacts extends Activity {    private ListVIEw mListVIEw;    private ProgressDialog pDialog;    private Handler updatebarHandler;    ArrayList<String> contactList;    Cursor cursor;    int counter;    button callContact;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.contacts_List_vIEw);        pDialog = new ProgressDialog(this);        pDialog.setMessage("Reading contacts...");        pDialog.setCancelable(false);        pDialog.show();        mListVIEw = (ListVIEw) findVIEwByID(R.ID.List);        updatebarHandler = new Handler();        callContact = (button) findVIEwByID(R.ID.callContact);        // Since reading contacts takes more time, let's run it on a separate thread.        new Thread(new Runnable() {            @OverrIDe            public voID run() {                getContacts();            }        }).start();        // Set onclickListener to the List item.        mListVIEw.setonItemClickListener(new OnItemClickListener() {            @OverrIDe            public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw,                                    int position, long ID) {                //Todo Do whatever you want with the List data                Toast.makeText(getApplicationContext(), "Item clicked : \n" + contactList.get(position), Toast.LENGTH_SHORT).show();            }        });    }    public voID getContacts() {        contactList = new ArrayList<String>();        String phoneNumber = null;        String email = null;        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;        String _ID = ContactsContract.Contacts._ID;        String disPLAY_name = ContactsContract.Contacts.disPLAY_name;        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;        Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;        String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;        String DATA = ContactsContract.CommonDataKinds.Email.DATA;        StringBuffer output;        ContentResolver contentResolver = getContentResolver();        cursor = contentResolver.query(CONTENT_URI, null, null, null, null);        // Iterate every contact in the phone        if (cursor.getCount() > 0) {            counter = 0;            while (cursor.movetoNext()) {                output = new StringBuffer();                // Update the progress message                updatebarHandler.post(new Runnable() {                    public voID run() {                        pDialog.setMessage("Reading contacts : " + counter++ + "/" + cursor.getCount());                    }                });                String contact_ID = cursor.getString(cursor.getColumnIndex(_ID));                String name = cursor.getString(cursor.getColumnIndex(disPLAY_name));                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));                if (hasPhoneNumber > 0) {                    output.append("\nname: " + name);                    //This is to read multiple phone numbers associated with the same contact                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_ID}, null);                    while (phoneCursor.movetoNext()) {                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));                        output.append("\n" + phoneNumber);                    }                    phoneCursor.close();                    // Read every email ID associated with the contact                    Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID + " = ?", new String[]{contact_ID}, null);                    while (emailCursor.movetoNext()) {                        email = emailCursor.getString(emailCursor.getColumnIndex(DATA));                        output.append("\n Email:" + email);                    }                    emailCursor.close();                }                // Add the contact to the ArrayList                contactList.add(output.toString());            }            // ListVIEw has to be updated using a ui thread            runOnUiThread(new Runnable() {                @OverrIDe                public voID run() {                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.List_item, R.ID.text1, contactList);                    mListVIEw.setAdapter(adapter);                }            });            // dismiss the progressbar after 500 millisecondds            updatebarHandler.postDelayed(new Runnable() {                @OverrIDe                public voID run() {                    pDialog.cancel();                }            }, 500);        }    }}

contacts_List_vIEw.xml
显示联系人列表

<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context="edu.utep.cs.cs4330.easytech.Home">    <ImageVIEw        androID:layout_wIDth="0dp"        androID:layout_height="0dp"        androID:scaleType="centerCrop"        androID:src="@drawable/back4"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintleft_toleftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constrainttop_totopOf="parent"        tools:layout_constraintBottom_creator="1"        tools:layout_constraintleft_creator="1"        tools:layout_constraintRight_creator="1"        tools:layout_constrainttop_creator="1" />    <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent"        androID:layout_margin="20dp">        <ListVIEw            androID:ID="@+ID/List"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content" />    </relativeLayout></androID.support.constraint.ConstraintLayout>

List_item.xml
每个单独的联系人

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    androID:paddingBottom="5dp"    androID:paddingtop="5dp">    <button        androID:ID="@+ID/callContact"        androID:layout_wIDth="125dp"        androID:layout_height="wrap_content"        androID:layout_alignParentRight="true"        androID:layout_centerInParent="true"        androID:text="Call" />    <TextVIEw        androID:ID="@+ID/text1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_toleftOf="@+ID/callContact"        androID:layout_aligntop="@+ID/callContact"        androID:layout_alignParentleft="true"        androID:textSize="16sp"        androID:textcolor="@androID:color/black" /></relativeLayout>

任何提示表示赞赏,谢谢.

解决方法:

这是正确的方法

意图意图=新意图(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));context.startActivity(intent);

不要忘记添加权限

总结

以上是内存溢出为你收集整理的java-如何从Android Studio中检索到的联系人列表中调用联系人?全部内容,希望文章能够帮你解决java-如何从Android Studio中检索到的联系人列表中调用联系人?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1091580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存