Android 如何在启动屏幕关闭时停止打开的活动 我有闪屏 打开应用程序后,启动屏幕将在启动屏幕传递给HomeActivity完成后显示 但当我在启动屏幕运行一段时间后关闭该应用程序时,主屏幕将自动打开,但我想关闭该应用程序 但是当我关闭应用程序时,主屏幕不应该显示

Android 如何在启动屏幕关闭时停止打开的活动 我有闪屏 打开应用程序后,启动屏幕将在启动屏幕传递给HomeActivity完成后显示 但当我在启动屏幕运行一段时间后关闭该应用程序时,主屏幕将自动打开,但我想关闭该应用程序 但是当我关闭应用程序时,主屏幕不应该显示,android,android-activity,android-handler,android-thread,Android,Android Activity,Android Handler,Android Thread,公共类动画扩展活动{ ImageView imageViewSplash; TextView txtAppName; 相对的相对的; 螺纹飞溅螺纹; 媒体播放器米松; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u splash\u视图); mySong=MediaPlayer.create(SplashAnimat

公共类动画扩展活动{
ImageView imageViewSplash;
TextView txtAppName;
相对的相对的;
螺纹飞溅螺纹;
媒体播放器米松;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u splash\u视图);
mySong=MediaPlayer.create(SplashAnimation.this,R.raw.monn);
mySong.start();
imageViewSplash=(ImageView)findViewById(R.id.imageViewSplash);
txtAppName=(TextView)findViewById(R.id.txtAppName);
relativeLayout=(relativeLayout)findViewById(R.id.relative);
startAnimations();
}
私有void startAnimations(){
动画旋转=AnimationUtils.loadAnimation(this,R.anim.translate);
Animation translate=AnimationUtils.loadAnimation(this,R.anim.translate);
旋转。重置();
translate.reset();
relativeLayout.clearAnimation();
imageViewSplash.startAnimation(旋转);
txtAppName.startAnimation(翻译);
SplashThread=新线程(){
@凌驾
公开募捐{
super.run();
int=0;
等待时间(等待时间<3500){
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
平均值+=100;
}
SplashAnimation.this.finish();
意向意向=新意向(SplashAnimation.this,LibraryView.class);
intent.setFlags(intent.FLAG\u活动\u无\u动画);
星触觉(意向);
mySong.stop();
}
};
splashtread.start();
}
@凌驾
受保护的void onStop(){
SplashAnimation.this.finish();
完成();
mySong.stop();
super.onStop();
}
@凌驾
受保护的空onDestroy(){
完成();
mySong.stop();
super.ondestory();
}

}
一旦您调用了
splashtread.start()
它将尽其所能完成它的工作。我建议改用
处理程序
,尽管您可以远程取消任务,但处理程序会运行:

//init and declare the handler instance
private Handler delayHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (delayHandler == null) {
        delayHandler = new Handler();
    }
    //your code
}

//define the task the handler should do
private void startAnimations() {
//replace the code beginning at 'Thread SplashThread = new Thread()' with the following
delayhandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
        //these flags will prevent to 'redo' the transition by hitting the back button, that also makes calling 'finish()' obsolete
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);            
        startActivity(intent);
    }
//instead of the while loop just execute the runnable after below given amount of milliseconds
}, 3500)

//to remotely cancel the runnable, if the app, respectively the Activity gets killed override 'onDestroy()'
@Override
public void onDestroy() {
    super.onDestroy();
    mySong.stop();
    //calling 'finish()' is obsolete, tho 'finish()' calls 'onDestroy()' itself
    //tell the handler to quit its job
    delayHandler.removeCallbacksAndMessages(null);
}
调用onStop()方法


您可以使用
Timer
而不是实例化线程类

请参阅下面的代码,在4秒钟后开始活动。在SplashActivity的
onCreate()
中使用此选项

timer = new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}, 4000);
在您的
onPause()
方法中,使用:

timer.cancel()

这将终止计时器并忽略任何当前计划的任务。

尝试将意向部分放入runOnUiThread()方法
onDestroy()
必须是公共的,因为它是回调方法,也是活动生命周期的一部分。如果它受到保护,它就不安全,需要时可以正确访问它启动屏幕在做什么?它正确播放音乐?原因是:java.lang.NullPointerException:尝试在com.monnfamily.libraryapp.UIActivity.SplashAnimation.onDestroy(SplashAnimation.java:94)delayHandler.removeCallbacksAndMessages(java.lang.Object)上的空对象引用上调用虚拟方法“void android.os.Handler.removeCallbacksAndMessages(java.lang.Object)”;您是否在
onCreate()
中定义了delayHandler?是的,我刚刚注意到,我的答案中有一个输入错误。确保到处都是delayHandler(而不是带有小h的delayHandler)。如果这不起作用,只需编写
private Handler delayHandler=new Handler()
并省略
onCreate()中的定义
timer.cancel()