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

相关文章

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++