热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

语音兰度短信

原理:获取来电短信内容,调用系统的语音朗读功能。效果图:具体代码如下:1,获取短信息:[html]viewplaincopypackagecom.internal.message;imp

原理:获取来电短信内容,调用系统的语音朗读功能。

效果图:


具体代码如下:

1,获取短信息:

[html] view plaincopy
  1. package com.internal.message;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.database.Cursor;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.provider.ContactsContract;  
  10. import android.telephony.SmsMessage;  
  11. import android.test.suitebuilder.annotation.SmallTest;  
  12.   
  13. public class SmsMessageReceiver extends BroadcastReceiver {  
  14.      
  15.     @Override  
  16.     public void onReceive(Context context, Intent intent) {  
  17.         Bundle extras = intent.getExtras();  
  18.         if (extras == null)  
  19.             return;  
  20.   
  21.         Object[] pdus = (Object[]) extras.get("pdus");  
  22.   
  23.             SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0]);  
  24.             String fromAddress = message.getOriginatingAddress();  
  25.             String fromDisplayName = fromAddress;  
  26.   
  27.             Uri uri;  
  28.             String[] projection;  
  29.   
  30.             uri = Uri.withAppendedPath(  
  31.                     ContactsContract.PhoneLookup.CONTENT_FILTER_URI,  
  32.                     Uri.encode(fromAddress));  
  33.             projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME };  
  34.   
  35.             // 查找联系人  
  36.             Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);  
  37.             if (cursor != null) {  
  38.                 if (cursor.moveToFirst())  
  39.                     fromDisplayName = cursor.getString(0);  
  40.   
  41.                 cursor.close();  
  42.                 cursor=null;  
  43.             }  
  44.   
  45.             // 启动消息显示器  
  46.             Intent di = new Intent();  
  47.             di.setClass(context, SmsReceivedDialog.class);  
  48.             di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  49.             di.putExtra(MsgAction.SMS_FROM_ADDRESS_EXTRA, fromAddress);  
  50.             di.putExtra(MsgAction.SMS_FROM_DISPLAY_NAME_EXTRA, fromDisplayName);  
  51.             di.putExtra(MsgAction.SMS_MESSAGE_EXTRA, message.getMessageBody().toString());  
  52.             context.startActivity(di);  
  53.     
  54.     }  
  55.       
  56. }  


2,注册广播:

[html] view plaincopy
  1. <receiver android:name="com.internal.message.SmsMessageReceiver" android:enabled="false">  
  2.             <intent-filter>  
  3.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
  4.             intent-filter>  
  5.         receiver>  


3,消息显示及朗读功能: [html] view plaincopy
  1. package com.internal.message;  
  2.   
  3. import java.util.Locale;  
  4.   
  5. import com.internal.main.R;  
  6.   
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.app.Dialog;  
  10. import android.content.DialogInterface;  
  11. import android.content.Intent;  
  12. import android.os.Bundle;  
  13. import android.speech.tts.TextToSpeech;  
  14. import android.speech.tts.TextToSpeech.OnInitListener;  
  15. import android.util.Log;  
  16.   
  17.   
  18. public class SmsReceivedDialog extends Activity implements OnInitListener {  
  19.       
  20.     private static final String TAG = "SmsReceivedDialog";  
  21.     private static final int DIALOG_SHOW_MESSAGE = 1;  
  22.   
  23.     private TextToSpeech mTts;  
  24.     private String mFromDisplayName;  
  25.     private String mFromAddress;  
  26.     private String mMessage;  
  27.     private String mFullBodyString;  
  28.   
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.   
  33.         mFromAddress = getIntent().getExtras().getString(MsgAction.SMS_FROM_ADDRESS_EXTRA);  
  34.         mFromDisplayName = getIntent().getExtras().getString(MsgAction.SMS_FROM_DISPLAY_NAME_EXTRA);  
  35.         mMessage = getIntent().getExtras().getString(MsgAction.SMS_MESSAGE_EXTRA);  
  36.   
  37.         mFullBodyString = String.format(  
  38.                 getResources().getString(R.string.sms_speak_string_format),  
  39.                 " "+mFromDisplayName,  
  40.                 "message said:"+mMessage);  
  41.   
  42.         showDialog(DIALOG_SHOW_MESSAGE);  
  43.         mTts = new TextToSpeech(this, this);  
  44.     }  
  45.   
  46.     public void onInit(int status) {  
  47.         if (status == TextToSpeech.SUCCESS) {  
  48.             int result = mTts.setLanguage(Locale.US);  
  49.             if (result == TextToSpeech.LANG_MISSING_DATA  
  50.                     || result == TextToSpeech.LANG_NOT_SUPPORTED) {  
  51.                 Log.e(TAG, "TTS language is not available.");  
  52.             } else {  
  53.                 mTts.speak(mFullBodyString, TextToSpeech.QUEUE_ADD, null);  
  54.             }  
  55.         } else {  
  56.             // Initialization failed.  
  57.             Log.e(TAG, "Could not initialize TTS.");  
  58.         }  
  59.     }  
  60.   
  61.     @Override  
  62.     protected Dialog onCreateDialog(int id) {  
  63.         switch (id) {  
  64.         case DIALOG_SHOW_MESSAGE:  
  65.             return new AlertDialog.Builder(this)  
  66.                     .setIcon(android.R.drawable.ic_dialog_email)  
  67.                     .setTitle("Message Received")  
  68.                     .setMessage(mFullBodyString)  
  69.                     .setPositiveButton("replay", new DialogInterface.OnClickListener() {  
  70.                         public void onClick(DialogInterface dialog, int whichButton) {  
  71.                               
  72.                             Intent i = new Intent();  
  73.                             i.setClass(SmsReceivedDialog.this, SendMsgActivity.class);  
  74.                             i.putExtra(MsgAction.SMS_RECIPIENT_EXTRA, mFromAddress);  
  75.                             startActivity(i);  
  76.   
  77.                             dialog.dismiss();  
  78.                             finish();  
  79.                         }  
  80.                     })  
  81.                     .setNegativeButton("display", new DialogInterface.OnClickListener() {  
  82.                         public void onClick(DialogInterface dialog, int whichButton) {  
  83.                             dialog.dismiss();  
  84.                             finish();  
  85.                         }  
  86.                     })  
  87.                     .setOnCancelListener(new DialogInterface.OnCancelListener() {  
  88.                         public void onCancel(DialogInterface dialog) {  
  89.                             finish();  
  90.                         }  
  91.                     }).create();  
  92.         }  
  93.         return null;  
  94.     }  
  95.       
  96.     @Override  
  97.     protected void onDestroy() {  
  98.         if (mTts != null) {  
  99.             mTts.stop();  
  100.             mTts.shutdown();  
  101.             mTts=null;  
  102.         }  
  103.         super.onDestroy();  
  104.     }  
  105.       
  106. }  


最后别忘了添加权限,要不不能读取信息呀!

 

[java] view plaincopy
  1. "android.permission.READ_PHONE_STATE"/>  
  2.     "android.permission.RECEIVE_SMS"/>  
  3.    "android.permission.READ_SMS"/>  
  4.    "android.permission.SEND_SMS" />  



推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
author-avatar
铲除飞网败类
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有