简体   繁体   English

当另一个正在运行的Java android时如何停止进程

[英]how to stop a process when another is running java android

i am doing an app in android that connecting with servers and downloading some chunks. 我正在用android做一个与服务器连接并下载一些块的应用程序。 now i want to have another one process doing other job. 现在我想让另一个过程完成其他工作。 here is my code 这是我的代码

class RemindTask extends TimerTask {


                    public void run() {

                       flag = true;
                       System.out.println(" Hello World!");
                        Receiveeterations reeterations = new Receiveeterations();
                         int longCounteterations = reeterations.Geteterations();
                         System.out.println("longCounteterations " + longCounteterations);

                       long endTime = System.currentTimeMillis() + 10000;

                        while (System.currentTimeMillis() < endTime) {


                               } 

                        IntegerResult = 4;
                        ThroughPut1+=1;
                        ThroughPut2=0;

                        //se mia methodo! pou otan oloklirwnetai i diadikasia tha ti kanei false    
                        flag=false;   
}

 Timer timer = new Timer();
  timer.schedule(new RemindTask(), 0,20000);

   ///////////////////////////////////////////////////////////////////////////////////////////////////  


                       try {
                            Thread.sleep(10010);
                        } catch (InterruptedException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                            }  
while (!fileparts.isEmpty()  ) { //&& !fileparts1.isEmpty() && (flag == false) 




    String[] myStringArray = new String[IntegerSpeed];

here i have more code with connectios

        }

so what i want to do and i don't know, is how when the timer class is triggered the while process will stop. 所以我想做的我不知道的是,如何触发计时器类,而while进程将停止。 and after the process of timer stops the main method continues. 计时器停止运行后,主要方法继续。 i try thread.sleep inside timer but it's not doing anything. 我尝试在计时器内部使用thread.sleep,但是它什么也没做。

all you need is a running flag... maybe that is what you have your flag variable for? 您只需要一个运行中的标志...也许这就是您要使用的flag变量的作用?

class RemindTask extends TimerTask {

    private boolean runnging = false;

    public void run() {
        running = true;
        [...]
        while (running && System.currentTimeMillis() < endTime) {
            // work
        }

    public void stop() {
        running = false;
        // interrupt the thread if possible
    }
}

now you can call stop() on your running task to make it stop 现在您可以在正在运行的任务上调用stop()以使其停止

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

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