RT 我写了一个SmsReceiver extends BroadcastReceiver类 已经获取到了短信的sender和body 可是怎么让内容显示在一个Activity中 并且在点击的情况下语音读出来? 还有就是我写的只能英文发音,设置成中文就不行了?求教。。。。。。。。。
// 监听短信内容的类 package com.fangwei.speaker; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.speech.tts.TextToSpeech; import android.telephony.SmsMessage; import android.widget.Toast; public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "短信到来", 1).show(); Object[] pdus=(Object[]) intent.getExtras().get("pdus"); for(Object pdu:pdus){ SmsMessage smsMessage=SmsMessage.createFromPdu((byte[])pdu); String body=smsMessage.getMessageBody(); String sender=smsMessage.getOriginatingAddress(); System.out.println("body:"+body); System.out.println("sender:"+sender); } } } // 可以读出EditText中内容的Activity package com.fangwei.speaker; import java.util.Locale; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.telephony.SmsMessage; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Build; public class MainActivity extends ActionBarActivity { private static EditText content; private static Button speak; static TextToSpeech tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tts = new TextToSpeech(this, new OnInitListener() { public void onInit(int status) { // 如果装载TTS引擎成功 if (status == TextToSpeech.SUCCESS) { // 设置使用美式英语朗读 int result = tts.setLanguage(Locale.US); // 如果不支持所设置的语言 if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE && result != TextToSpeech.LANG_AVAILABLE) { Toast.makeText(MainActivity.this, "TTS暂时不支持这种语言的朗读。", Toast.LENGTH_LONG).show(); } } } }); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } public void onDestroy() { // 关闭TextToSpeech对象 if (tts != null) { tts.shutdown(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); content=(EditText)rootView.findViewById(R.id.content); speak=(Button)rootView.findViewById(R.id.speak); speak.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // 执行朗读 tts.speak(content.getText().toString(), TextToSpeech.QUEUE_ADD, null); } }); return rootView; } } }
短信接收界面?不知道你用过百度语音助手没有。
你可以接收收到短信的广播,然后得到短信内容,然后朗读出来。
这个过程我知道,但我是个新手,不知道怎么去实现这个过程,希望有大神可以帮我实现出来