【问题标题】:How to stop handler or thread如何停止处理程序或线程
【发布时间】:2011-06-24 13:49:11
【问题描述】:

我的程序是随机字符串 首先我按下按钮开始,然后它会随机 其次,我再次按下相同的按钮使其停止我该怎么办??

我的代码

package com.Randomsentence;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Randomsentence extends Activity {
  protected static final int CONTINUE = 0;
  protected static final int STOP = 0;
  TextView txt;
  int time = 30;
  int random;
  public String[] myString;
  Button bt1;
  boolean check = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt=(TextView)findViewById(R.id.txt);
    bt1 = (Button)findViewById(R.id.bt1);
    bt1.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        thread.start();
      }
    });
  }

  // our handler
  Handler handler = new Handler() {
    public void handleMessage(Message msg) {//display each item in a single line
      {
        if (check == false) {
          Random rgenerator = new Random();
          Resources res = getResources();
          myString = res.getStringArray(R.array.myArray);
          String q = myString[rgenerator.nextInt(myString.length)];
          txt.setText(q);
        }
      }
    }
  };

  Thread thread = new Thread() {
    @Override
    public void run() {
      try {
        while(true) {
          sleep(1000);
          handler.sendMessage(handler.obtainMessage());
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  };
}

这是 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginBottom="40px" android:layout_marginLeft="40px" android:layout_marginRight="40px" android:layout_marginTop="40px">
<TextView android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:text="TextView"
android:id="@+id/txt" android:layout_width="fill_parent"
android:layout_marginRight="40px"
android:layout_marginTop="50px"></TextView>
<Button android:text="Button" android:layout_height="wrap_content"
android:id="@+id/bt1" android:layout_width="wrap_content"
android:layout_below="@+id/txt" android:layout_alignLeft="@+id/txt"
android:layout_alignRight="@+id/txt" android:layout_marginLeft="40px"
android:layout_marginRight="40px" android:layout_marginTop="40px"></Button>
</RelativeLayout>
</LinearLayout>

这是插入到文件夹值的array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myArray">
<item>ข้าว</item>
<item>ข้าว</item>
<item>เส้นใหญ่</item>
<item>เส้นเล็ก</item>
<item>วุ้นเส้น</item>
<item>ข้าวผัด</item>
<item>เส้นใหญ่</item>
<item>ข้าว</item>
<item>มักกะโรนี</item>
<item>ผัดผัก</item>
<item>ต้มยำ</item>
<item>ทอดกระเทียม</item>
<item>ผัดพริกเผา</item>
<item>ผัดกระเพรา</item>
<item>ผัดน้ำมันหอย</item>
<item>ต้มยำแห้ง</item>
<item>ทอดกระเทียม</item>
<item>แพนง</item>
<item>ผัดกระเพรา</item>
</string-array>
</resources> 

【问题讨论】:

  • 这么多代码……你确定都需要吗?请总结您的问题。

标签: java android mysql database


【解决方案1】:

实际上,不建议停止线程,让操作系统为您完成。您应该在while 循环中使用flag 变量而不是true,并在要停止线程时将其更改为false。关于Handlers - 它们无法停止,因为它们只是为了同步线程而设计的,所以不用担心它们。

【讨论】:

  • 你能显示代码吗?我对安卓很陌生。所以很迷茫
  • 我想再问一个问题。如果我想随机字符串,当我按下按钮停止它。我希望文字慢慢停止同样的彩票
  • 布尔标志 = true; while(flag) { // 你的线程在这里工作 } flag = false; //当你需要停止线程我根本不明白你的第二个问题。
【解决方案2】:

你必须使用一些标志,这将决定什么时候 setRandom 什么时候不 初始化布尔变量

boolean showRandom = false;

在按钮单击时设置其值如下

类似下面的

 @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    //          tv.setHint(tv.getHint());
    //          while(counter<2){
                showRandom = !showRandom;
                System.out.println("..show random.."+showRandom);
                t = new Thread() {
                    public void run() {
    //                  register();
                        try {
                            while(showRandom){
                                System.out.println("..print dialog..");
   sleep(1000);
handler.sendMessage(handler.obtainMessage());


    //                        }
                            }

                }catch(Exception ex){
                    ex.printStackTrace();
                }
                    }
                };
                t.start();

                }

    //      }
        });

【讨论】:

  • 我想再问一个问题。如果我想随机字符串,当我按下按钮停止它。我希望文本慢慢停止相同的彩票。我该怎么做
  • 嗯,我明天会测试,因为我现在没有 sumsung Galaxy 测试它
  • 第二次按下按钮停止时如何通过慢文本随机播放
  • 为什么双击按钮时程序会暂停。
猜你喜欢
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多