Java 如何从Android Studio中检索到的联系人列表中呼叫联系人?
此代码允许我从用户的手机中检索联系人列表并显示它们。我正在做一些修改,我为每个联系人添加了一个“呼叫”按钮,但我很难理解如何只检索电话号码。一旦我得到了电话号码,我会这样做来拨打该号码:Java 如何从Android Studio中检索到的联系人列表中呼叫联系人?,java,android,Java,Android,此代码允许我从用户的手机中检索联系人列表并显示它们。我正在做一些修改,我为每个联系人添加了一个“呼叫”按钮,但我很难理解如何只检索电话号码。一旦我得到了电话号码,我会这样做来拨打该号码: Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("0377778888")); 我知道打哪个号码有多准确?如何知道x用户有y号码,然后将该号码传递给目标用户 下面是代码: Contacts.jav
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);
}
}
}
包edu.utep.cs.cs4330.easytech;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.ContentResolver;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Handler;
导入android.provider.contacts合同;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类联系人扩展活动{
私有列表视图;
私人对话;
私有处理程序updateBarHandler;
ArrayList联系人列表;
光标;
整数计数器;
按钮呼叫触点;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts\u list\u视图);
pDialog=新建进度对话框(此对话框);
pDialog.setMessage(“正在读取联系人…”);
pDialog.setCancelable(假);
pDialog.show();
mListView=(ListView)findViewById(R.id.list);
updateBarHandler=新处理程序();
callContact=(按钮)findViewById(R.id.callContact);
//因为读取联系人需要更多的时间,所以让我们在单独的线程上运行它。
新线程(newrunnable()){
@凌驾
公开募捐{
getContacts();
}
}).start();
//将onclicklistener设置为列表项。
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//TODO可对列表数据执行任何操作
Toast.makeText(getApplicationContext(),“单击的项目:\n”+contactList.get(位置),Toast.LENGTH\u SHORT.show();
}
});
}
公共联系人(){
contactList=新的ArrayList();
字符串phoneNumber=null;
字符串email=null;
Uri CONTENT\u Uri=Contacts contract.Contacts.CONTENT\u Uri;
String _ID=Contacts contact.Contacts.\u ID;
String DISPLAY\u NAME=Contacts contract.Contacts.DISPLAY\u NAME;
字符串HAS\u PHONE\u NUMBER=Contacts contract.Contacts.HAS\u PHONE\u NUMBER;
Uri PhoneCONTENT\u Uri=ContactsContract.CommonDataTypes.Phone.CONTENT\u Uri;
字符串Phone\u CONTACT\u ID=contacts contract.commondatatypes.Phone.CONTACT\u ID;
字符串编号=contacts contract.commonDataTypes.Phone.NUMBER;
Uri EmailCONTENT\u Uri=ContactsContract.CommonDataTypes.Email.CONTENT\u Uri;
字符串EmailCONTACT_ID=contacts contract.commondatatypes.Email.CONTACT_ID;
字符串数据=ContactsContract.CommonDataTypes.Email.DATA;
字符串缓冲输出;
ContentResolver ContentResolver=getContentResolver();
cursor=contentResolver.query(CONTENT\u URI,null,null,null);
//迭代电话中的每个联系人
if(cursor.getCount()>0){
计数器=0;
while(cursor.moveToNext()){
输出=新的StringBuffer();
//更新进度消息
updateBarHandler.post(新的Runnable(){
公开募捐{
pDialog.setMessage(“正在读取联系人:“+counter++++”/“+cursor.getCount()”);
}
});
String contact_id=cursor.getString(cursor.getColumnIndex(_id));
字符串名称=cursor.getString(cursor.getColumnIndex(DISPLAY_name));
int hasPhoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER));
如果(hasPhoneNumber>0){
output.append(“\n名称:+name”);
//这是为了读取与同一联系人关联的多个电话号码
游标phoneCursor=contentResolver.query(PhoneCONTENT\u URI,null,Phone\u CONTACT\u ID+“=?”,新字符串[]{CONTACT\u ID},null);
while(phoneCursor.moveToNext()){
phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
输出。追加(“\n”+电话号码);
}
phoneCursor.close();
//阅读与联系人关联的每个电子邮件id
Cursor emailCursor=contentResolver.query(EmailCONTENT_URI,null,EmailCONTACT_ID+“=?”,新字符串[]{contact_ID},null);
while(emailCursor.moveToNext()){
email=emailCursor.getString(emailCursor.getColumnIndex(数据));
输出。追加(“\n电子邮件:“+电子邮件”);
}
emailCursor.close();
}
//将联系人添加到ArrayList
contactList.add(output.toString());
}
//必须使用ui线程更新ListView
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
ArrayAdapter=新的ArrayAdapter(getApplicationContext(),R.layout.list_项,R.id.text1,contactList);
mListView.setAdapter(适配器);
<?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>
<?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.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);
// A dilaog that will open when a contact is clicked, verify that you want to delete the contact.
protected void areYouSureDialog(final SelectUser tmpUser) {
final String userPhoneNumber = tmpUser.getPhone();
new AlertDialog.Builder(this)
.setTitle("Call a Contact")
.setMessage("Are you sure you want to call:" + userPhoneNumber)
.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callUser(userPhoneNumber);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing.
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
public void callUser(String phoneNum) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNum + ""));
if (ActivityCompat.checkSelfPermission(CallActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
<uses-permission android:name="android.permission.CALL_PHONE" />