我有一项服务,只要屏幕打开就会运行。当我让手机进入睡眠状态时,服务和线程应该停止。
我设法停止了服务,但线程仍在运行。我怎样才能杀死线程?
void runAppCheck(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getForegroundApplication();
compareResults();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenIsOn = pm.isScreenOn();
if (!screenIsOn) {
Log.i(TAG ,"Screen is OFF, stopping service");
stopSelf();
//TODO stop thread
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();