杀死所有线程android后退出活动

时间:2016-07-07 10:52:22

标签: java android multithreading android-intent

我正在尝试使用进度条。我有这个(下面)简单的活动,当我调用Progress(N)时,它会一个接一个地运行进度条N次。它工作得很好,但我面临的问题是,如果我按下后退按钮。我进入mainActivity,但进度条(线程)仍在后台依次运行。一旦他们完成了N个循环,就会调用intent,而我将要做的任何事情都会被这个LOOP_OVER活动中断。

我试过自己解决这个问题。我尝试使用Thread类的变量(在我直接执行之前)。并尝试在onDestroy()中或者甚至在调用intent之前中断()但它没有帮助。我该怎么办呢?

public class Loop extends Activity {

    private ProgressBar progressBar;
    private CircleProgress circleProgress;
    private int progressStatus = 0;
    private Handler handler = new Handler();

    private TextView myView;
    private int started = 0, doneLoop=0;
    private Thread th;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loop);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        circleProgress = (CircleProgress) findViewById(R.id.circle_progress);

        myView = (TextView) findViewById(R.id.instruction);

        progressBar.setScaleY(3f);
        // Start long running operation in a background thread

        Progress(3);

    }

    @Override
    public void onDestroy() {
        // Below, everything I am just 
        th.interrupt();
        Loop.this.finish();
        Thread.currentThread().interrupt();
        super.onDestroy();
    }

    public void Progress(final int numberOfRuns){
//        QueView.setText(Que);
        if(numberOfRuns == 0){
            th.interrupt();
            Intent myIntent = new Intent(Loop.this, LOOP_OVER.class);
            startActivity(myIntent);
            super.onDestroy();
            finish();
        }
        th = new Thread(new Runnable() {
            public void run() {
                genNextSet();
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            circleProgress.setProgress(progressStatus);
                            progressBar.setProgress(progressStatus);
                            textView.setText(progressStatus+"/"+progressBar.getMax());
                        }
                    });
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub

                                myView.setText(Que);
                            }
                        });
                        // Sleep for 200 milliseconds.
                        //Just to display the progress slowly
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                progressStatus = 0;

                Progress(numberOfRuns - 1);
            }
        });

        th.start();
    }


    private void genNextSet() {
        // so some cool here!
    }
}

3 个答案:

答案 0 :(得分:1)

您可以想到在所有线程之间共享的类变量。

尝试添加以下内容:

private Boolean LOOP = true;

然后

while (progressStatus < 100 && LOOP) {

@Override
public void onBackPressed() { 
    LOOP = false
}

if(LOOP == true){
    // call intent
}
finish();

答案 1 :(得分:0)

如果您按下&#34;后退&#34; -key,您的活动不会被销毁,因此onDestroy()将不会被调用。
我将覆盖{{1} },如果我在哪里。
或者,您可以尝试将其放入onBackPressed() - 方法。

答案 2 :(得分:0)

你没有覆盖按下的后退按钮..这个

packagingOptions {

    exclude 'META-INF/LICENSE-FIREBASE.txt'
}

dependencies {

compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services:9.0.2'
compile 'com.google.firebase:firebase-core:9.0.2'

}

apply plugin: 'com.google.gms.google-services'