语音兰度短信详解手机开发

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

效果图:

语音兰度短信详解手机开发

具体代码如下:

1,获取短信息:

  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,注册广播:

  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,消息显示及朗读功能:

  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. }  

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

 <uses-permission android:name=”android.permission.READ_CONTACTS” />

  1. <uses-permission android:name = “android.permission.READ_PHONE_STATE”/>  
  2.     <uses-permission android:name=“android.permission.RECEIVE_SMS”/>  
  3.    <uses-permission android:name=“android.permission.READ_SMS”/>  
  4.    <uses-permission android:name=“android.permission.SEND_SMS” />  

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5609.html

(0)
上一篇 2021年7月16日 23:56
下一篇 2021年7月16日 23:56

相关推荐

发表回复

登录后才能评论