语音兰度短信
来源:互联网 发布:淘宝客网站怎么做 编辑:程序博客网 时间:2024/04/29 01:09
原理:获取来电短信内容,调用系统的语音朗读功能。
效果图:
具体代码如下:
1,获取短信息:
- package com.internal.message;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.telephony.SmsMessage;
- import android.test.suitebuilder.annotation.SmallTest;
- public class SmsMessageReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Bundle extras = intent.getExtras();
- if (extras == null)
- return;
- Object[] pdus = (Object[]) extras.get("pdus");
- SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0]);
- String fromAddress = message.getOriginatingAddress();
- String fromDisplayName = fromAddress;
- Uri uri;
- String[] projection;
- uri = Uri.withAppendedPath(
- ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
- Uri.encode(fromAddress));
- projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME };
- // 查找联系人
- Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
- if (cursor != null) {
- if (cursor.moveToFirst())
- fromDisplayName = cursor.getString(0);
- cursor.close();
- cursor=null;
- }
- // 启动消息显示器
- Intent di = new Intent();
- di.setClass(context, SmsReceivedDialog.class);
- di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- di.putExtra(MsgAction.SMS_FROM_ADDRESS_EXTRA, fromAddress);
- di.putExtra(MsgAction.SMS_FROM_DISPLAY_NAME_EXTRA, fromDisplayName);
- di.putExtra(MsgAction.SMS_MESSAGE_EXTRA, message.getMessageBody().toString());
- context.startActivity(di);
- }
- }
2,注册广播:
- <receiver android:name="com.internal.message.SmsMessageReceiver" android:enabled="false">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- package com.internal.message;
- import java.util.Locale;
- import com.internal.main.R;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.tts.TextToSpeech;
- import android.speech.tts.TextToSpeech.OnInitListener;
- import android.util.Log;
- public class SmsReceivedDialog extends Activity implements OnInitListener {
- private static final String TAG = "SmsReceivedDialog";
- private static final int DIALOG_SHOW_MESSAGE = 1;
- private TextToSpeech mTts;
- private String mFromDisplayName;
- private String mFromAddress;
- private String mMessage;
- private String mFullBodyString;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mFromAddress = getIntent().getExtras().getString(MsgAction.SMS_FROM_ADDRESS_EXTRA);
- mFromDisplayName = getIntent().getExtras().getString(MsgAction.SMS_FROM_DISPLAY_NAME_EXTRA);
- mMessage = getIntent().getExtras().getString(MsgAction.SMS_MESSAGE_EXTRA);
- mFullBodyString = String.format(
- getResources().getString(R.string.sms_speak_string_format),
- " "+mFromDisplayName,
- "message said:"+mMessage);
- showDialog(DIALOG_SHOW_MESSAGE);
- mTts = new TextToSpeech(this, this);
- }
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- int result = mTts.setLanguage(Locale.US);
- if (result == TextToSpeech.LANG_MISSING_DATA
- || result == TextToSpeech.LANG_NOT_SUPPORTED) {
- Log.e(TAG, "TTS language is not available.");
- } else {
- mTts.speak(mFullBodyString, TextToSpeech.QUEUE_ADD, null);
- }
- } else {
- // Initialization failed.
- Log.e(TAG, "Could not initialize TTS.");
- }
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG_SHOW_MESSAGE:
- return new AlertDialog.Builder(this)
- .setIcon(android.R.drawable.ic_dialog_email)
- .setTitle("Message Received")
- .setMessage(mFullBodyString)
- .setPositiveButton("replay", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Intent i = new Intent();
- i.setClass(SmsReceivedDialog.this, SendMsgActivity.class);
- i.putExtra(MsgAction.SMS_RECIPIENT_EXTRA, mFromAddress);
- startActivity(i);
- dialog.dismiss();
- finish();
- }
- })
- .setNegativeButton("display", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.dismiss();
- finish();
- }
- })
- .setOnCancelListener(new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- finish();
- }
- }).create();
- }
- return null;
- }
- @Override
- protected void onDestroy() {
- if (mTts != null) {
- mTts.stop();
- mTts.shutdown();
- mTts=null;
- }
- super.onDestroy();
- }
- }
最后别忘了添加权限,要不不能读取信息呀!
<uses-permission android:name="android.permission.READ_CONTACTS" />
- <uses-permission android:name = "android.permission.READ_PHONE_STATE"/>
- <uses-permission android:name="android.permission.RECEIVE_SMS"/>
- <uses-permission android:name="android.permission.READ_SMS"/>
- <uses-permission android:name="android.permission.SEND_SMS" />
0 0
- 语音兰度短信
- 短信验证+语音识别
- (短信、彩信、语音)协议
- 如何设置短信语音播报
- 来电和短信语音播报
- Android短信收到,语音播报
- 短信验证or语音验证
- BREW中语音与短信的开发
- app用户手机验证:短信还是语音?
- 兰
- 互动式语音应答业务IVR能否续写短信神话
- 云之讯语音、短信验证码实现
- 短信验证码和语音验证码的优劣对比
- 最好用的发短信(验证码、语音短信)接口(PHP)
- 最好用的发短信(验证码、语音短信)接口(PHP)
- 语音识别置信度
- 语音
- 语音
- 小型mvc框架开发
- 【字符串】ZigZag Conversion
- 电话拦截
- vmware vcenter 5.5无法登陆
- 文本特征提取方法研究
- 语音兰度短信
- linux学习之环境配置
- LeetCode || Max Points on a Line
- MFC API——》GetCurrentTime ()
- (基于Java)算法之动态规划——矩阵连乘问题
- jquery get post ajax getjson区别
- JAVA过滤html标签的方法
- ASP.NET架构下的网页布局
- linux学习之邮件服务器