本文主要是介绍Android_system_调用系统联系人拨打电话,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
tips:github上的项目链接:https://github.com/chengbiao1314/android_system_getContacts.git
获取系统联系人三步,外加拨打电话:
1、添加权限:
<uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" />
2、开启系统联系人界面
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);
3、处理返回结果
if (resultCode == Activity.RESULT_OK) {ContentResolver reContentResolverol = getContentResolver();Uri contactData = data.getData();@SuppressWarnings("deprecation")Cursor cursor = managedQuery(contactData, null, null, null, null);cursor.moveToFirst();name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,null,null);while (phone.moveToNext()) {num = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}et_name.setText(name);et_num.setText(num);
}
Ps:拨打电话:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num));
startActivity(callIntent);
完整代码如下:(需要在清单文件中添加两个权限)
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="10dp"tools:context=".MainActivity"android:orientation="vertical"><EditTextandroid:id="@+id/et_name"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="plaease input your name..."/><EditTextandroid:id="@+id/et_phone"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="plaease input your phone num..."/><Buttonandroid:id="@+id/btn_getContacts"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="get num form system..."/><Buttonandroid:id="@+id/btn_call"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:hint="call the num..."/></LinearLayout>
代码:
package com.example.ricky.android_system_getcontacts;import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends Activity {private EditText et_name;private EditText et_num;private Button btn_getContacts;private Button btn_calling;private String name;private String num;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_name = (EditText)findViewById(R.id.et_name);et_num = (EditText)findViewById(R.id.et_phone);btn_getContacts = (Button) findViewById(R.id.btn_getContacts);btn_calling = (Button) findViewById(R.id.btn_call);btn_getContacts.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);}});btn_calling.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num));startActivity(callIntent);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == Activity.RESULT_OK) {ContentResolver reContentResolverol = getContentResolver();Uri contactData = data.getData();@SuppressWarnings("deprecation")Cursor cursor = managedQuery(contactData, null, null, null, null);cursor.moveToFirst();name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,null,null);while (phone.moveToNext()) {num = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}et_name.setText(name);et_num.setText(num);}}
}
这篇关于Android_system_调用系统联系人拨打电话的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!