首页新闻找找看学习计划

Android 怎么在短信来到时自动读出短信内容?已经完成部门,上代码

0
悬赏园豆:50 [已解决问题] 解决于 2017-08-29 10:53

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

}
MyCPlusPlus的主页 MyCPlusPlus | 初学一级 | 园豆:57
提问于:2014-05-16 14:40
< >
分享
最佳答案
0

短信接收界面?不知道你用过百度语音助手没有。

你可以接收收到短信的广播,然后得到短信内容,然后朗读出来。

收获园豆:50
achu.me | 小虾三级 |园豆:877 | 2014-05-16 15:42

这个过程我知道,但我是个新手,不知道怎么去实现这个过程,希望有大神可以帮我实现出来

MyCPlusPlus | 园豆:57 (初学一级) | 2014-05-16 17:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册