简体   繁体   中英

multi thread programming in android

I am new in android programming. So I have an ArrayList<String> and i want to speech in a loop and when i click a button , speech stop and if i click same button , speech start again.

My layout is something like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnPauseResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />

</LinearLayout>

and my java code is like this :

public class Activity_test extends Activity {

    Button btnPauseResume = null;

    boolean IsPaused = false;

    private TextToSpeech tts = null;

    ArrayList<String> Texts = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);

        btnPauseResume = (Button) findViewById(R.id.btnPauseResume);

        Texts = new ArrayList<String>();

        btnPauseResume.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {

                        Activity_test.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                IsPaused = !IsPaused;
                                if (IsPaused) {
                                    btnPauseResume.setText("Resume");
                                } else {
                                    btnPauseResume.setText("Pause");
                                    Start();
                                }
                            }
                        });
                    }
                }).start();

            }
        });

        Start();
    }

    public void Start() {

        new Thread(new Runnable() {
            public void run() {
                Texts.clear();
                Texts.add("Long Text 1");
                Texts.add("Long Text 2");
                Texts.add("Long Text 3");

                Speech();
            }
        }).start();
    }

    public void Speech() {
        tts = new TextToSpeech(Activity_test.this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                // TODO Auto-generated method stub
                if (status == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.ENGLISH);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(Activity_test.this, "This Language is not supported", Toast.LENGTH_SHORT).show();
                    } else {
                        for (String Text : Texts) {
                            if (!IsPaused) {
                                tts.speak(Text, TextToSpeech.SUCCESS, null);
                                while (tts.isSpeaking()) {
                                    //wait till speech finish
                                }
                            }
                        }
                        if (!IsPaused) {
                            Start();
                        }
                    }
                } else
                    Toast.makeText(Activity_test.this, "Initilization Failed!", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

in real code the array list of text is changed so this is the reason i call the method Speach() to load new ArrayList to speech.

so the problem is when i click on the button it dose not work (and back button too).

Try replacing your Threads with Handlers and Runnables. See http://developer.android.com/reference/android/os/Handler.html

I would also make sure that the code in the OnClickListener is actually invoked (using debug/log).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM