如何才去新的活动来杀死一个线程和处理程序 [英] How to kill a thread and handler before going to new activity

查看:158
本文介绍了如何才去新的活动来杀死一个线程和处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿 - 这code可能是我试图清理,因为我一直在试图追查那里的崩溃发生......处理程序的方式有点乱

我已经得到了正显示出由一个线程和处理程序进行动画与进度输入密码的对话活动...

看来,当我试图看到,如果进度完成,并试图杀死线程,我这样做是搞乱了的东西的方式,当我尝试去一个新的活动 - 即在方法调用一个函数,而不是有什么回报的的或东西...

 公共类RMO_Dialog延伸活动{
    私人进度progbar;
    私人按钮dialogOK;
    私人的EditText dialogPass;
    私人共享preferences preFS;
    私人字符串传递;
    私人INT增量= 10;
    私人主题背景;    私人布尔commCalled = FALSE;    公共无效callCommunications(){
        progbar.setVisibility(0);
        progbar.setProgress(0);
        background.stop();
        Toast.makeText(getApplicationContext(),来电来函应该发生一次。Toast.LENGTH_LONG).show();
//意图I =新意图();
// i.setClass(RMO_Dialog.this,RMO_Comm.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(ⅰ);
//完成();
    }    公共无效buzzUser(){        振动器V =(振动器)getSystemService(Context.VIBRATOR_SERVICE);
        INT点= 200;
        INT破折号= 500;
        INT short_gap = 200;
        INT medium_gap = 500;
        INT long_gap = 1000;
        长[]模式= {0,点,short_gap,点,short_gap,圆点,medium_gap,破折号,short_gap,破折号,short_gap,破折号,medium_gap,点,short_gap,
                点,short_gap,圆点,long_gap};        v.vibrate(图案,-1);
    }    公共无效killCountdown(){
        progbar.setVisibility(0);
        progbar.setProgress(0);
        background.stop();
    }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.dialogpassword);        buzzUser();        preFS = this.getShared preferences(RMO,MODE_WORLD_READABLE);
        通过= prefs.getString(密码,);        dialogOK =(按钮)findViewById(R.id.dialogOK);
        dialogPass =(EditText上)findViewById(R.id.dialogPass);
        progbar =(进度)findViewById(R.id.progress);        progbar.setProgress(0);        背景=新主题(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    而(progbar.getProgress()&下; = progbar.getMax()){
                        视频下载(300);
                        progressHandler.sendMessage(progressHandler.obtainMessage());
                    }
                }赶上(java.lang.InterruptedException E){
                    Toast.makeText(getApplicationContext(),错误抛出。Toast.LENGTH_LONG).show();
                }            }        });
        background.start();        dialogOK.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                如果(dialogPass.getText()。的toString()。等于(pass.toString())){
                    killCountdown();
                    Toast.makeText(getApplicationContext(),守护天使接下来的警报已经解除。Toast.LENGTH_LONG).show();
                    意向意图=新的Intent();
                    intent.setClass(RMO_Dialog.this,RMO.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(意向);
                    完();
                }其他{
                    callCommunications();
                }
            }
        });    }    处理器progressHandler =新的处理程序(){
        公共无效的handleMessage(消息MSG){
            progbar.incrementProgressBy(增量);
            如果(progbar.getProgress()== progbar.getMax()){
                Toast.makeText(getApplicationContext(),commcalled:+ commCalled,Toast.LENGTH_LONG).show();
                如果(commCalled == FALSE){
                    commCalled = TRUE;
                    callCommunications();
                }            }
        }
    };
}


解决方案

使用Thread.stop是德precated通话,而应该使用Thread.interrupt方法。

 公共无效killCountdown(INT WAITTIME){
    progbar.setVisibility(0);
    progbar.setProgress(0);
    //德precated:background.stop();
    background.interrupt(); //< - 确定
    background.join(WAITTIME); //可选等待线程退出
}

了Thread.interrupt 将导致 ThreadInterruptedException 下一次你的线程块或睡觉,你就已经在处理你的线程体,这样很好。此外,您可能要包括挥发性标志,让你停下来时,它不会阻止或睡眠线程,但这是可选的。

Hey all - this code might be a bit messy in the way I'm trying to clean up the handler as I've been trying to track down where the crash happens...

I've got a dialog activity that is showing a password entry with a progressbar being animated by a thread and handler...

It seems that when I'm attempting to see if the progressbar is done, and trying to kill the thread, the way I'm doing this is messing something up when I try to go to a new activity - ie in a way calling a function and not having anything to return to or something...

public class RMO_Dialog extends Activity {
    private ProgressBar progbar;
    private Button dialogOK;
    private EditText dialogPass;
    private SharedPreferences prefs;
    private String pass;
    private int increment=10;
    private Thread background;

    private Boolean commCalled=false;

    public void callCommunications(){
        progbar.setVisibility(0);
        progbar.setProgress(0);
        background.stop();
        Toast.makeText(getApplicationContext(), "Call communication should happen once.", Toast.LENGTH_LONG).show();
//      Intent i = new Intent();
//      i.setClass(RMO_Dialog.this, RMO_Comm.class);
//      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//      startActivity(i);
//          finish();
    }

    public void buzzUser(){

        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        int dot = 200;
        int dash = 500;
        int short_gap = 200;
        int medium_gap = 500;
        int long_gap = 1000;
        long[] pattern = {0,dot, short_gap, dot, short_gap, dot, medium_gap, dash, short_gap, dash, short_gap, dash, medium_gap, dot, short_gap, 
                dot, short_gap, dot, long_gap};

        v.vibrate(pattern, -1);


    }

    public void killCountdown(){
        progbar.setVisibility(0);
        progbar.setProgress(0);
        background.stop();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogpassword);

        buzzUser();

        prefs = this.getSharedPreferences("RMO", MODE_WORLD_READABLE);
        pass = prefs.getString("password", "");

        dialogOK = (Button) findViewById(R.id.dialogOK);
        dialogPass = (EditText) findViewById(R.id.dialogPass);
        progbar = (ProgressBar) findViewById(R.id.progress);

        progbar.setProgress(0);

        background = new Thread(new Runnable(){
            @Override
            public void run() {
                try{
                    while(progbar.getProgress()<=progbar.getMax()){
                        Thread.sleep(300);
                        progressHandler.sendMessage(progressHandler.obtainMessage());
                    }
                }catch(java.lang.InterruptedException e){
                    Toast.makeText(getApplicationContext(), "Error thrown.", Toast.LENGTH_LONG).show();
                }

            }

        });
        background.start();

        dialogOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogPass.getText().toString().equals(pass.toString())){
                    killCountdown();
                    Toast.makeText(getApplicationContext(), "Guardian Angel next alert has been disengaged.", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(RMO_Dialog.this, RMO.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }else{
                    callCommunications();
                }
            }
        });



    }

    Handler progressHandler = new Handler(){
        public void handleMessage(Message msg){
            progbar.incrementProgressBy(increment);
            if(progbar.getProgress()==progbar.getMax()){
                Toast.makeText(getApplicationContext(), "commcalled: "+ commCalled, Toast.LENGTH_LONG).show();
                if(commCalled==false){
                    commCalled=true;
                    callCommunications();
                }

            }
        }
    };
}

解决方案

Thread.stop is deprecated call, instead you should use the Thread.interrupt method.

public void killCountdown(int waitTime){
    progbar.setVisibility(0);
    progbar.setProgress(0);
    // deprecated: background.stop();
    background.interrupt(); // <-- OK
    background.join(waitTime); // optionally wait for the thread to exit
}

Thread.Interrupt will cause a ThreadInterruptedException next time your thread blocks or sleeps and you're already handling in your thread body so that's good. Additionally, you might want to include a volatile flag that will allow you to stop the thread when it's not blocking or sleeping, but that's optional.

这篇关于如何才去新的活动来杀死一个线程和处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆