如何将字符串传递给MainActivity,然后再次启动onCreate?

时间:2015-07-22 11:26:01

标签: java android android-studio

在MainActivity.java中我有:

package com.dan.webservertest;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Locale;



public class MainActivity extends ActionBarActivity
{
    private static final int MY_DATA_CHECK_CODE = 0;

    TextToSpeech mTts;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTTS();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Dispatch onStart() to all fragments.  Ensure any created loaders are
     * now started.
     */
    @Override
    protected void onStart()
    {
        super.onStart();


        TextToSpeechServer.main(null);
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    public void initTTS() {
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == MY_DATA_CHECK_CODE) {
            if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                mTts = new TextToSpeech(getApplicationContext(), new OnInitListener() {
                    @Override
                    public void onInit(int i) {
                        if(i == TextToSpeech.SUCCESS) {
                            int result = mTts.setLanguage(Locale.US);
                            if(result == TextToSpeech.LANG_AVAILABLE
                                    || result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                                mTts.setPitch(1);
                                mTts.speak(textforthespeacch, TextToSpeech.QUEUE_FLUSH, null);
                            }
                        }
                    }
                });
            } else {
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

    public static String textforthespeacch = "";
    public static void TextToSpeak(String text) {
        textforthespeacch = text;
    }


}

它正在调用并运行initTTS一次。 然后它正在运行onStart

这是TexttoSpeechServer代码的上半部分:

@Override public Response serve(IHTTPSession session) {
        Method method = session.getMethod();
        String uri = session.getUri();
        System.out.println(method + " '" + uri + "' ");

        String msg = "<html><body><h1>Hello server</h1>\n";
        Map<String, String> parms = session.getParms();



        if (parms.get("say") != null) {
            msg += "<p>Hello, " + parms.get("say") + "!</p>"; 
            MainActivity.TextToSpeak(parms.get("say"));

例如,如果参数说= hello world 然后在MainActivity中我看到:变量textoforthespeacch被赋予了hellow world。

public static String textforthespeacch = "";
    public static void TextToSpeak(String text) {
        textforthespeacch = text;
    }

现在问题是在为textforthespeacch分配了文本之后,例如:hello world如何再次调用/ run / init方法initTTS()

或许我应该以某种方式打电话onCreate

问题是我的方法TextToSpeak是静态的,initTTS()不是静态的,我不能使initTTS静态。 onCreate也不是静态的。

那么每次将文本传递给TextToSpeech变量时,如何再次调用/ run / init /激活textforthespeacch部分?

0 个答案:

没有答案