原理:获取来电短信内容,调用系统的语音朗读功能。
效果图:
具体代码如下:
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>
3,消息显示及朗读功能:
- 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” />
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5609.html