文章主要讲的是以前做项目时实现的来短信或来电话时语音播报的功能实现,记录下来做个总结。 1.实现语音播报的关键 -- 语音播报工具类(TtsUtil.java) 为了方便

文章主要讲的是以前做项目时实现的来短信或来电话时语音播报的功能实现,记录下来做个总结,,其中有些数据库的值是自己在其他地方加的开关。

1.实现语音播报的关键 -- 语音播报工具类(TtsUtil.java)

package com.android.mms.transaction;

import android.content.Context;

import android.provider.Settings;

import android.speech.tts.TextToSpeech;

import android.speech.tts.UtteranceProgressListener;

import android.speech.tts.TextToSpeech.OnInitListener;

import android.util.Log;

public class TtsUtil {

public static TextToSpeech mTts;

public static void read(Context ctx, final String content) {

Log.d("dwj",

"messageRead = "

+ Settings.Secure.getInt(ctx.getContentResolver(),

"hands_free_mode", 0));

if (Settings.Secure.getInt(ctx.getContentResolver(), "hands_free_mode",

0) == 0) {

return;

}

if (null != mTts) {

mTts.stop();

try {

mTts.shutdown();

} catch (Exception e) {

e.printStackTrace();

}

}

mTts = new TextToSpeech(ctx, new OnInitListener() {

@Override

public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {

if (null != mTts) {

mTts.setSpeechRate(1.0f);

mTts.speak(content, TextToSpeech.QUEUE_FLUSH, null);

mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

@Override

public void onDone(String utteranceId) {

mTts.stop();

mTts.shutdown();

}

@Override

public void onError(String utteranceId) {

mTts.stop();

mTts.shutdown();

}

@Override

public void onStart(String utteranceId) {

}

});

} else {

Log.e("RFLauncher", "Cann't create TextToSpeech object");

}

}

}

});

}

}

2.1 短信接收时播报

SmsReceiverService.java中的handleSmsReceived()函数中添加即可:

StringBuffer SMSAddress = new StringBuffer();

StringBuffer SMSContent = new StringBuffer();

Bundle bundle = intent.getExtras();

if(bundle != null){

Object[] pdusObjects = (Object[])bundle.get("pdus");

SmsMessage[] messages = new SmsMessage[pdusObjects.length];

for(int i=0; i

messages[i] = SmsMessage.createFromPdu((byte[])pdusObjects[i]);

}

for(SmsMessage message : messages){

SMSAddress.append(message.getDisplayOriginatingAddress()); // send number

//SMSContent.append(message.getDisplayMessageBody()); // send content

}

}

String senderName = getContactNameFromPhoneNum(getApplicationContext()

,SMSAddress.toString());

if(Settings.Secure.getInt(getContentResolver(), "hands_free_message", 0) == 1){

if(senderName.equals("")){

TtsUtil.read(getApplicationContext(), getResources()

.getString(R.string.new_message_from)+ SMSAddress.toString());

}else{

if(IsNum(senderName)){

String speekName = senderName.replaceAll(".{1}(?!$)", "$0 ");

TtsUtil.read(getApplicationContext(), getResources()

.getString(R.string.new_message_from) + speekName);

} else {

TtsUtil.read(getApplicationContext(), getResources()

.getString(R.string.new_message_from) + senderName);

}

}

}

2.2 短信对话框弹出时播报

DialogModeActivity.java中的setDialogView()函数中添加即可:

if(Settings.Secure.getInt(getContentResolver(), "hands_free_message", 0) == 1){

TtsUtil.read(this, getResources().getString(R.string.new_message_from)

+getSenderString());

}

3. 来电时的语音播报

InCallScreen.java中:

public String callScreenDisplayName = null;

public int mCountTTS;

onStart()函数中添加:

mCountTTS = 0;

if(TtsUtil.mTts == null){

TtsUtil.mTts = new TextToSpeech(getApplicationContext(), null);

}

inComingCallTTS();

inComingCallTTS()是自己实现的函数(代码大概是用来实现来电几秒钟后开始播报来电信息的):

public void inComingCallTTS() {

if (mCountTTS == 3) {

return;

} else {

final HashMap myHashAlarm = new HashMap();

TelephonyManager tm = (TelephonyManager)this.getApplicationContext()

.getSystemService(Service.TELEPHONY_SERVICE);

myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "end of wakeup message ID");

if (Settings.Secure.getInt(getApplicationContext().getContentResolver(), "hands_free_call", 0) == 1

&& tm.getCallState() == TelephonyManager.CALL_STATE_RINGING) {

Timer mTimer = new Timer();

mTimer.schedule(new TimerTask() {

@Override

public void run() {

// TODO Auto-generated method stub

++mCountTTS;

final CallNotifier notifier = mApp.notifier;

if (notifier.isRinging()) {

notifier.silenceRinger();

}

TtsUtil.mTts.speak(getApplicationContext().getString(

R.string.new_calling_from) + callScreenDisplayName,

TextToSpeech.QUEUE_FLUSH, myHashAlarm);

// TtsUtil.read(getApplicationContext(),getApplicationContext()

.getString(R.string.new_calling_from) + callScreenDisplayName);

TtsUtil.mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

@Override

public void onUtteranceCompleted(String utteranceId) {

// TODO Auto-generated method stub

if (utteranceId.equals("end of wakeup message ID")) {

notifier.restartRinger();

Timer mCountTimer = new Timer();

mCountTimer.schedule(new TimerTask() {

@Override

public void run() {

inComingCallTTS();

}

}, 0 * 1000);

}

}

});

}

}, 4 * 1000);

}

}

}

最后,在onStop()函数中添加:mCountTTS= 0;即可。

就总结到这里了。

android 语音自动播报,Android 语音播报实现相关推荐

  1. android 实现自动拍照,android实现定时拍照功能

    在手机上面实现,设置一段时间(以秒计时)之后,自动拍照,适用于摄影师建立一个场景,之后设置时间,再进入场景. 界面主要就是一个设置时间的EditText和启动倒计时的Button,设置完时间之后,点击 ...

  2. android设置自动亮度,Android亮度调节的几种实现方法

    最近在做一个App的设置项,亮度调节.真正做时,发现Android亮度调节比预想要复杂一些.其实目前网上已有不少这方面的资料,但有些博文具有一定误导性.在此将这块内容按照自己理解整理一下. 整体上看, ...

  3. android 键盘 自动消失,android 键盘状态,获取键盘显示和隐藏

    要设置弹出键盘是否覆盖Activity的view,或者软键盘的显示隐藏状态,需要用到Activity的一个属性: android:windowSoftInputMode 该属性在AndroidMani ...

  4. android edittext自动获取焦点,Android取消EditText自动获取默认焦点

    Android取消EditText自动获取默认焦点 发布时间:2020-10-02 14:08:30 来源:脚本之家 阅读:142 作者:ganchuanpu 最近在通讯录新建联系人=中,一进入一个页 ...

  5. android 应用自动重启,Android:如何在“强制关闭”后自动重启应用程序?

    Android:如何在"强制关闭"后自动重启应用程序? 在Android应用程序中,如果我们没有得到正确的例外,我们通常会收到"强制关闭"错误. 如果强行关闭, ...

  6. android文字自动滚动,Android TextView文字横向自动滚动(跑马灯)

    TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true" 2.设置可滚到,或显示样式:android:elli ...

  7. android notification自动消失,Android开发 -- 状态栏通知Notification、NotificationManager详解...

    本想自己写一个的,但是看到这篇之后,我想还是转过来吧,实在是非常的详细: 在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? ...

  8. android 360自动更新,Android应用自动更新功能的实现!!!

    大家好,发现半年没有更新博客了,最近一直都比较忙,决定在凌晨 英超 阿森纳VS富勒姆 中场休息的时候,给大家分享Android里应用版本更新功能这一块的实现. 一个好的应用软件都是需要好的维护,从初出 ...

  9. android开发自动拍照,Android:Camera2开发详解(上):实现预览、拍照、保存照片等功能...

    android.jpg 前言 在前几篇文章中介绍了如何调用系统相机拍照和使用Camera1的实现自定义相机拍照.人脸检测等功能 文章传送门: 接下来的几篇文章中,我将给大家介绍如何使用Camera2实 ...

  10. android+tv+自动切换,Android TV 重写GridView,实现焦点放大效果

    关于缩放,使用了view.setScaleX/Y 方法,api11以上即可. 重写dispatchDraw(),绘制选中项的焦点效果.(注意带阴影的焦点图需要微调偏移量) 要将选中项绘制显示在顶层,所 ...

最新文章

  1. android 找不到符号 符号 RequiresApi
  2. 机器学习(MACHINE LEARNING)MATLAB蚁群算法解决TSP问题
  3. 使用C#开发一个简单的P2P应用
  4. Phpstorm数组对齐设置
  5. 前端学习(799):根据位置返回字符
  6. idea 下划线字段转驼峰_Java如何实现数据库中表字段的下划线和驼峰式命名的Model相互转换,很方便的...-Go语言中文社区...
  7. 蝴蝶优化算法_腾讯机智团队分享--AllReduce算法的前世今生
  8. python3第八天(面向对象)
  9. ssh登录到esxi机器中后开关虚拟机
  10. 业务逻辑?到底是什么
  11. [android]实现拖动效果
  12. 如何写好一篇技术博客
  13. Mybatis使用关联查询由于表名字段重复导致的问题
  14. Good Bye 2020 E
  15. NAIPC2018 Zoning Houses(ST表)
  16. crout分解计算例题_如何计算有理函数的不定积分
  17. AIX(Advanced Interactive eXecutive)操作系统
  18. 2021年中国电力行业境外电力项目签约额分析:实现签约项目536个 签约合同总金额440.6亿美元[图]
  19. C#设计程序购买商品总金额
  20. 前端框架系列之(eslint入门)

热门文章

  1. codevs——1958 刺激
  2. hashtable、hashmap、ConcurrentHashMap、treemap的区别
  3. IE9下iframe显示异常的处理方法
  4. POJ 2186 Popular Cows
  5. 认识计算机系统反思,《认识计算机系统》教学反思
  6. oracle ddl会被什么阻塞,MySQL Online DDL与DML并发阻塞关系总结
  7. 电容和频率的关系_为什么会有直流电和交流电?频率为什么是50hz?白话科普专业知识...
  8. uni 根目录路径_如何解决uniapp图片路径错误问题
  9. 7打开pycharm_Python+pycharm安装、关联教程
  10. oracle votedisk 参数,11g r2 rac votedisk 及 ocr 磁盘破坏后,基于ocr备份的恢复步骤