Android_system_调用系统联系人拨打电话

2024-08-25 22:38

本文主要是介绍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_调用系统联系人拨打电话的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1106846

相关文章

linux系统中java的cacerts的优先级详解

《linux系统中java的cacerts的优先级详解》文章讲解了Java信任库(cacerts)的优先级与管理方式,指出JDK自带的cacerts默认优先级更高,系统级cacerts需手动同步或显式... 目录Java 默认使用哪个?如何检查当前使用的信任库?简要了解Java的信任库总结了解 Java 信

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

Oracle数据库在windows系统上重启步骤

《Oracle数据库在windows系统上重启步骤》有时候在服务中重启了oracle之后,数据库并不能正常访问,下面:本文主要介绍Oracle数据库在windows系统上重启的相关资料,文中通过代... oracle数据库在Windows上重启的方法我这里是使用oracle自带的sqlplus工具实现的方

使用Go调用第三方API的方法详解

《使用Go调用第三方API的方法详解》在现代应用开发中,调用第三方API是非常常见的场景,比如获取天气预报、翻译文本、发送短信等,Go作为一门高效并发的编程语言,拥有强大的标准库和丰富的第三方库,可以... 目录引言一、准备工作二、案例1:调用天气查询 API1. 注册并获取 API Key2. 代码实现3

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境