如何在没有崩溃的情况下中断线程onBackPressed?

时间:2015-09-29 17:38:10

标签: android splash-screen java-threads

我已经尝试重写onBackPressed,不仅要完成当前活动,还要中断线程,因为我调用了对下一个活动的意图。当我之前按下时,启动活动结束,但线程继续运行并将意图调用到下一个活动。既然我已经在onBackPressed上包含了线程中断,那么当我按下时应用程序崩溃了。我做错了什么?

skipscreen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            SharedPreferences savestate = getSharedPreferences("skip", MODE_PRIVATE);
            savestate.edit().putBoolean("skip", true).apply();
            skip= true;
            homeintent();
        }
    });
    Thread splashthread = new Thread() {
        @Override
        public void run() {
                try {
                    sleep(3000);
                    if (!skip) { homeintent();}
                } catch (InterruptedException Interrupt) {
                    Interrupt.printStackTrace();
                }
            }
    };
    splashthread.start();
}
private void homeintent() {
    Intent i = new Intent(splash.this, home.class);
    startActivity(i);
    finish();
}
@Override
public void onBackPressed() {
    splashthread.interrupt();
    finish();
  }
}

更新:(此解决方案对我有用)。

skipscreen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            SharedPreferences savestate = getSharedPreferences("skip", MODE_PRIVATE);
            savestate.edit().putBoolean("skip", true).apply();
            skip= true;
            homeintent();
        }
    });
    Thread splashthread = new Thread() {
        @Override
        public void run() {
                try {
                    sleep(3000);
                    if (!skip) { homeintent();}
                } catch (InterruptedException Interrupt) {
                    Interrupt.printStackTrace();
                }
            }
    };
    splashthread.start();
}
private void homeintent() {
    if (!ThreadInterrupted) {
           Intent i = new Intent(splash.this, home.class);
           startActivity(i);
   }
           finish();
}
@Override
public void onBackPressed() {
        SharedPreferences savestate= getSharedPreferences("ThreadInterrupted", MODE_PRIVATE);
        savestate.edit().putBoolean("ThreadInterrupted", true).apply();
        ThreadInterrupted = true;
        homeintent();
    }
}

4 个答案:

答案 0 :(得分:1)

当您在线程上调用interrupt()方法时,在休眠状态下,将抛出InterruptedException。

您可以在线程内查看天气应继续:

 Thread splashthread = new Thread() {
    @Override
    public void run() {
           while (shouldContinue) {
                  doSomeWork();
           }
   }
};

然后,您可以在onBackPressed方法中将shouldContinue更改为false:

@Override
public void onBackPressed() {
       shouldContinue = false;
       finish();
}

答案 1 :(得分:1)

为了给你一个简单的答案以及解释今后如何做到这一点,请看一下Handler课程和Handler.postDelayed()方法。

 private Handler homeIntenthandler;
    @Override
    protected void onCreate(){

    //other things
        skipscreen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                SharedPreferences savestate = getSharedPreferences("skip", MODE_PRIVATE);
                savestate.edit().putBoolean("skip", true).apply();
                skip= true;
                homeintent();
            }
        });

//initialise handler and set timer;
        homeIntenthandler= new Handler();
        homeIntenthandler.postDelayed(new Runnable() {
            @Override
            public void run() {
               homeintent();
            }
        },3000);
    }
    }
    private void homeintent() {
        Intent i = new Intent(splash.this, home.class);
        startActivity(i);
        finish();
    }
    @Override
    public void onBackPressed() {
      if(homeIntenthandler!=null)
        homeIntenthandler.removeCallbacksAndMessages(null)
        finish();
      }
    }

Handler.removeCallbacksAndMessages(null)基于same

的文档

答案 2 :(得分:0)

Udi我已经回答了你的问题。 但我建议您使用TimerTaskTimer代替Thread。

Timer timer;
TimerTask timertask;
timer = new Timer();
timertask = new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               homeintent();        
            }
        });
    }
};
timer.schedule(timertask, 3000);

@Override
public void onBackPressed() {
    if(timertask!=null){
        timertask.cancel();
    }
    if(timer!=null){
        timer.cancel();
    }
    finish();
}

答案 3 :(得分:0)

您可以创建易失性 boolean检查以阻止线程进展,请参阅以下代码:

private volatile boolean cancelled = false;

Thread splashthread = new Thread() {
    @Override
    public void run() {
            if ( cancelled ) {
              return; // or handle this however you want
            }else{
              // do what ever you want to do here
            }
        }
};
splashthread.start();

onBackPressed中执行以下操作:

@Override
public void onBackPressed() {
    cancelled = true;
    finish();
}

建议 volatile关键字,因为通常,执行取消的线程不一定与被取消的线程相同。将boolean检查标记为volatile可确保检查该标志的线程将看到最新值。