如何使用onClickListener禁用线程?

时间:2015-09-25 19:04:22

标签: android onclicklistener splash-screen

我正在进行这个启动画面测试,并对布局实现了一个点击监听器,因此可以通过单击屏幕跳过它。问题是意图被调用两次,因为单击屏幕时没有正确中断加载线程。我做错了什么以及如何避免这个错误?

public class SplashScreen extends Activity {
    final Thread Loading = new Thread();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.splashscreen);
    ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation);
    SplashScreenView.setBackgroundResource(R.drawable.flashscreenanimation);
    AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground();
    SplashScreenAnimation.start();
    RelativeLayout OnTouchSkipScreen = (RelativeLayout)findViewById(R.id.SplashScreenView);
    OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Loading.interrupt();
            Loading.isInterrupted();
            Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
            startActivity(SplashScreen);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            finish();
        }
    });
    Thread Loading = new Thread() {
        public void run() {
            try {
                sleep(2573);
                Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
                startActivity(SplashScreen);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                finish();
            }
        }
    };
    Loading.start();
}
}

1 个答案:

答案 0 :(得分:1)

去吧

public class SplashScreen extends Activity {
    Thread Loading ;
    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.splashscreen);
        ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation);
        SplashScreenView.setBackgroundResource(R.drawable.bontactbook);
        AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground();
        SplashScreenAnimation.start();
        RelativeLayout OnTouchSkipScreen = (RelativeLayout) findViewById(R.id.flashscreenanimation);
        OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flag = false;
                Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
                startActivity(SplashScreen);
                finish();
            }
        });
        Thread Loading = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                    if (flag) {
                        Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
                        startActivity(SplashScreen);
                        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    finish();
                }
            }
        };
        Loading.start();
    }
}