闪屏被杀死时如何停止开放活动

时间:2018-12-13 12:01:20

标签: android android-activity android-handler android-thread

  1. 我有启动画面。
  2. 打开我的应用程序后,启动画面完成传递给HomeActivity的意图后,就会出现启动画面。
  3. 但是当我在启动屏幕运行一段时间后杀死该应用程序时,HomeScreen将自动打开,但我想杀死该应用程序。
  4. 但是当我杀死该应用程序时,不应显示HomeScreen。

public class SplashAnimation extends Activity {

    ImageView imageViewSplash;
    TextView txtAppName;
    RelativeLayout relativeLayout;
    Thread SplashThread;
    MediaPlayer mySong;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_view);


        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();
    }

    private void startAnimations() {

        Animation rotate = AnimationUtils.loadAnimation(this, R.anim.translate);
        Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);

        rotate.reset();
        translate.reset();
        relativeLayout.clearAnimation();

        imageViewSplash.startAnimation(rotate);
        txtAppName.startAnimation(translate);
        SplashThread = new Thread() {
            @Override
            public void run() {
                super.run();
                int waited = 0;
                while (waited < 3500) {
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    waited += 100;
                }
                SplashAnimation.this.finish();
                Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                mySong.stop();
            }
        };
        SplashThread.start();
    }

    @Override
    protected void onStop() {

        SplashAnimation.this.finish();
        finish();
        mySong.stop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        finish();
        mySong.stop();
        super.onDestroy();
    }
}

3 个答案:

答案 0 :(得分:1)

一旦您致电SplashThread.start(),它将尽其所能。我建议改用Handler,以便您可以远程取消任务,处理程序将运行:

//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);
}

答案 1 :(得分:1)

调用onStop()方法

SplashThread.interrupt()

答案 2 :(得分:0)

您可以使用newStr.join(i.lower())而不是实例化Thread类。

请参考下面的代码,在4秒钟后启动“活动”。在SplashActivity的Timer中使用它。

onCreate()

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

onPause()

这将终止计时器,并且不考虑任何当前计划的任务。