简体   繁体   English

我如何加入该线程以使其停止?

[英]How do I join the thread to stop it?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mProgressBar = (ProgressBar)findViewById(R.id.adprogress_progressBar);


    final Thread timerThread = new Thread() {

        private volatile boolean running = true;
        public void terminate() {
            running = false;
        }
        @Override
        public void run() {
            while(running) {
            mbActive = true;
                try {
                int waited = 0;
                    while(mbActive && (waited < TIMER_RUNTIME)) {
                    sleep(200);
                        if(mbActive) {
                            waited += 200;
                            updateProgress(waited);
                        }
                    }
                } catch(InterruptedException e) {
                running=false;
                }
            }
        }
    };
    timerThread.start();
}

public void onLocationChanged(Location location) {

    if (location != null) {

        TextView text;
        text = (TextView) findViewById(R.id.t2);
        String str= "Latitude is " + location.getLatitude() + "\nLongitude is " + location.getLongitude();

        text.setText(str);
        text.postInvalidate();
    }

}

How would I stop the thread in onCreate from onLocationChanged? 如何从onLocationChanged停止onCreate中的线程? I need to stop the progressbar once the GPS provides the coordinates. GPS提供坐标后,我需要停止进度栏。 I need to join the threads using join(). 我需要使用join()加入线程。 Solution will be helpful. 解决方案将有所帮助。

使timerThread为类成员,而不是语言环境变量,通过这种方式,您应该从onLocationChanged方法访问它

改用AsyncTask,存储Future并自己停止线程。

If this is not a home work assignment, then I see no need to join() . 如果这不是家庭作业,那么我看不到join() Even more so when you're trying to join the UI thread with an arbitrary thread, effectively casuing an ANR . 更重要的是,当您尝试将UI线程与任意线程连接时,会有效地引起ANR

Either: 要么:

  1. Create you own class extending Thread , implement your terminate() method, and then call it whenever you want. 创建您自己的扩展Thread的类,实现您的terminate()方法,然后在需要时调用它。

  2. Create your own class extending AsyncTask , implementing LocationListener, and use its onProgressUpdate() method. 创建扩展AsyncTask的自己的类,实现LocationListener,并使用其onProgressUpdate()方法。

You can simply declare in your Activity a member : 您只需在Activity中声明一个成员:

private Thread mTimerThread = null;

then in your onCreate() replace : 然后在您的onCreate()中替换:

final Thread timerThread = new Thread() {

with

mTimerThread = new Thread() {

and in onLocationChanged : 并在onLocationChanged中:

if (mTimerThread != null && mTimerThread.isAlive()) {
    mTimerThread.terminate();
}

to achieve what you want. 实现您想要的。

However, as others mentioned, I would also recommend using custom AsyncTask as it would be most clear way of threading in your case. 但是,正如其他人提到的那样,我也建议您使用自定义AsyncTask因为这将是您情况下最清晰的线程处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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