-1

thread/timer戻るボタンを押したら、アプリを停止して終了させたいです。timer.stop();内部のタイマーonBackPressed()が、使用したのと同じローカル変数であることをどのように伝え public void run()ますか?

コード:

Thread timer = new Thread(){
        public void run(){
                try{
                    sleep(4400);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }finally{ 
                    Intent openHome = new Intent(Splash.this, main.class);
                    startActivity(openHome);
                    finish();
                }
            }
        };
    timer.start();
    }
        public void onBackPressed(){
            timer.stop();
        }

これを入力すると、'timer' in timer.stop(); cannot be resolved.

4

1 に答える 1

2

Thread.stop()は推奨されておらず、使用しないでください。より良いSplashscreen実装はHandler-Runnable、以下のようなものを使用することです。

private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
  public void run(){
    Intent openHome = new Intent(Splash.this, main.class);
    startActivity(openHome);
    finish();
  }
}

public void onCreate(Bundle b) {
  super.onCreate(b);
  handler.postDelayed(runnable, 4400);
}

public void onBackPressed(){
  handler.removeCallbacks(runnable);
  super.onBackPressed();
}
于 2013-09-11T20:00:18.073 に答える