【问题标题】:how to stop open activity while splash screen killed如何在启动画面被杀死时停止打开的活动
【发布时间】:2019-05-14 15:26:59
【问题描述】:
  1. 我有启动画面。
  2. 一旦我打开我的应用程序,启动屏幕将在完成将 Intent 传递给 HomeActivity 后出现。
  3. 但是当我在启动画面运行一段时间后杀死这个应用程序时,HomeScreen 会自动打开,但我想杀死这个应用程序。
  4. 但是当我关闭应用程序时,主屏幕不应该显示。

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

【问题讨论】:

  • 尝试将 Intent 部分放入 runOnUiThread() 方法
  • onDestroy() 必须是public,因为它是一个回调方法并且是 Activtiy 生命周期的一部分。如果它受到保护,它就是不安全的,它可以在需要时正确访问
  • 启动画面在做什么?它播放音乐对吗?

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


【解决方案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);
}

【讨论】:

  • 引起:java.lang.NullPointerException:尝试在 com.monnfamily.libraryapp 的空对象引用上调用虚拟方法 'void android.os.Handler.removeCallbacksAndMessages(java.lang.Object)' .UIActivity.SplashAnimation.onDestroy(SplashAnimation.java:94)
  • delayHandler.removeCallbacksAndMessages(null);
  • 你在onCreate()中定义了delayHandler吗?
  • 是的,我已经初始化了
  • 我刚刚注意到,我的回答中有错字。确保它到处都是延迟处理程序(而不是小 h 的延迟处理程序)。如果这不起作用,只需编写 private Handler delayHandler = new Handler(); 并省略 onCreate() 中的定义
【解决方案2】:

调用 onStop() 方法

SplashThread.interrupt()

【讨论】:

    【解决方案3】:

    您可以使用Timer 而不是实例化 Thread 类。

    参考下面的代码在 4 秒后启动 Activity。在 SplashActivity 的 onCreate() 中使用它。

    timer = new Timer().schedule(new TimerTask() {          
        @Override
        public void run() {
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    }, 4000);
    

    在您的onPause() 方法中使用:

    timer.cancel()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      相关资源
      最近更新 更多