本文主要是介绍Android 通过阿里语音实现打电话功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一:捋清楚打电话的逻辑
语音实现打电话功能,说难不难,说简单不简单,从表面现象来看,打电话其实很简单,但是其中还是很多奥秘的,要想把打电话,得先把打电话的逻辑思考好,打电话的逻辑分为三种情况:
(1) 当通讯录里名字只有一个电话号码的时候
说:打电话给 XX 时
回:正在为您拨打 XX 电话
呼叫:跳转到手机拨打电话的界面
取消:回答录音界面
(2) 当通讯录里面有多个名字多个号码的时
说:打电话给 XX 时
回:找到多个联系人,请选择第几个
说:第一个
回:正在为你拨打 XX 的电话
取消:回答录音界面
(3) 当通讯录没有名字的时候
说:打电话给 XX
回: 找不到联系人,请再说一遍
注意: 在语音识别到打电话的时候,必须先动态申请打电话的权限,如果没有申请权限,就要用一个友好的提示框提示用户先申请权限。
附加上以上三种情况的原型图,便于理解
第一种情况
第二种情况
二:实现功能
话不多说,先贴一个查找通讯录的工具类
****打电话使用的类**********/public class PhoneUtils {public static boolean matchPhone(String value) {Pattern p = Pattern.compile("[幺零一二三四五六七八九万亿壹贰叁肆伍陆柒捌玖萬]{3,}");Matcher m = p.matcher(value);if (m.matches()) {return true;} else {return false;}}//电话为数字public static void CallPhoneByNumber(Context context, String phone) {//String phoneNumber = getPhoneNumber(phone);Intent intent = new Intent(Intent.ACTION_CALL);Uri data = Uri.parse("tel:" + phone);intent.setData(data);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {return;}intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent);}public static String getPhoneNumber(String phone) {StringBuffer buffer = new StringBuffer();char[] chars = phone.toCharArray();for (int i = 0; i < chars.length; i++) {buffer.append(getNumber(chars[i] + ""));}return buffer.toString();}public static String getNumber(String value) {String phone = "";switch (value) {case "幺":case "一":case "壹":phone = "1";break;case "贰":case "二":phone = "2";break;case "三":case "叁":phone = "3";break;case "肆":case "四":phone = "4";break;case "伍":case "五":phone = "5";break;case "陆":case "六":phone = "6";break;case "柒":case "七":phone = "7";break;case "捌":case "八":phone = "8";break;case "玖":case "九":phone = "9";break;case "萬":case "万":phone = "0000";break;case "零":phone = "0";break;default:phone = "";break;}return phone;}//电话为名称public static void CallPhoneByName(Context context, String name) {Intent intent = new Intent(Intent.ACTION_CALL);Uri data = Uri.parse("tel:" + name);intent.setData(data);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {return;}intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent);}/*** 通过名字来查找通讯录** @param value* @return*/public static ArrayList<PhoneInfo> doNameFindContacts(Context context, String value) {ArrayList<PhoneInfo> phoneInfos = new ArrayList<>();Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);if (cursor != null) {int count = 0;while (cursor.moveToNext()) {String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));String contactsName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));if (Pattern.compile(PinYin.getReplaceString(value)).matcher(PinYin.getPinYin(contactsName)).find()) {Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);if (phone != null) {while (phone.moveToNext()) {String contactsNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));PhoneInfo info = new PhoneInfo();info = new PhoneInfo();info.setName(contactsName);info.setNumber(contactsNumber);info.setIndex(count);phoneInfos.add(info);count++;}}}}cursor.close();}return phoneInfos;}//判断手机是否安装某应用public static boolean isAppInstall(Context context, String packageName) {PackageInfo packageInfo;try {packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);} catch (PackageManager.NameNotFoundException e) {packageInfo = null;e.printStackTrace();}if (packageInfo == null) {return false;} else {return true;}}}
/****************************** 模糊查询 ************************/
public class HanziToPinyin3 {private static final String TAG = "HanziToPinyin";// Turn on this flag when we want to check internal data structure.private static final boolean DEBUG = false;/*** Unihans array.* <p/>* Each unihans is the first one within same pinyin when collator is zh_CN.*/public static final char[] UNIHANS = {'\u963f', '\u54ce', '\u5b89', '\u80ae', '\u51f9', '\u516b','\u6300', '\u6273', '\u90a6', '\u52f9', '\u9642', '\u5954','\u4f3b', '\u5c44', '\u8fb9', '\u706c', '\u618b', '\u6c43','\u51ab', '\u7676', '\u5cec', '\u5693', '\u5072', '\u53c2','\u4ed3', '\u64a1', '\u518a', '\u5d7e', '\u66fd', '\u66fe','\u5c64', '\u53c9', '\u8286', '\u8fbf', '\u4f25', '\u6284','\u8f66', '\u62bb', '\u6c88', '\u6c89', '\u9637', '\u5403','\u5145', '\u62bd', '\u51fa', '\u6b3b', '\u63e3', '\u5ddb','\u5205', '\u5439', '\u65fe', '\u9034', '\u5472', '\u5306&
这篇关于Android 通过阿里语音实现打电话功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!