如何禁用带有 onClickListener 的线程?
How can I disable a thread with an onClickListener?
我正在做这个闪屏测试,并为布局实现了一个点击监听器,这样就可以通过点击屏幕来跳过它。问题是 Intent 被调用了两次,因为当点击屏幕时 Loading 线程没有被正确中断。我做错了什么以及如何避免此错误?
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();
}
}
加油
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();
}
}
我正在做这个闪屏测试,并为布局实现了一个点击监听器,这样就可以通过点击屏幕来跳过它。问题是 Intent 被调用了两次,因为当点击屏幕时 Loading 线程没有被正确中断。我做错了什么以及如何避免此错误?
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();
}
}
加油
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();
}
}