3

我尝试覆盖 onBackPressed 以不仅完成当前活动,而且在我调用下一个活动的意图时中断线程。当我之前按下时,启动活动完成,但线程继续运行并将意图调用到下一个活动。现在我已经在BackPressed 上包含了线程中断,当我按下返回时应用程序崩溃了。我究竟做错了什么?

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

4 回答 4

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)是基于相同的文档

于 2015-09-29T18:32:13.657 回答
1

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

你可以检查你的线程天气它应该继续:

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

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

@Override
public void onBackPressed() {
       shouldContinue = false;
       finish();
}
于 2015-09-29T17:48:31.037 回答
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();
}
于 2015-09-29T18:16:03.937 回答
0

您可以创建一个volatile 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 关键字,因为通常执行取消的线程不一定是被取消的同一线程。将检查标记booleanvolatile确保检查标志的线程将看到最新值。

于 2015-09-29T18:29:43.857 回答