简体   繁体   English

如何让活动活动一段时间,然后另一个活动开始?

[英]How to make an Activity alive for certain time and after that another activity starts?

how to create an activity that is restricted to time. 如何创建一个受时间限制的活动。 I would like when I launch my app, an initial activity appears and lasts only for few seconds just to display the app name and the its version and some other info and after a the designated time, the app starts. 我希望当我启动我的应用程序时,会出现一个初始活动,只持续几秒钟,只显示应用程序名称及其版本和其他一些信息,并在指定时间后启动应用程序。 I have done such thing but the initial activity had had an animation, and when the animation ends then the new activity or the app starts. 我已经完成了这样的事情,但初始活动有一个动画,当动画结束时,新活动或应用程序启动。 But, now my initial activity has no animation and I do not know how to keep an activity active/live for 10 seconds, for an example, and when the 10 seconds end, another app starts? 但是,现在我的初始活动没有动画,我不知道如何将活动保持活动/活动10秒,例如,当10秒结束时,另一个应用程序启动?

Use a handler to wait (for example 3 seconds) and move on: 使用处理程序等待(例如3秒)并继续:

    private static int SPLASH_TIME_OUT = 3000;

    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with timer running. This is useful to showcase your app logo/company or something like that.
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);

You can use a Timer: 你可以使用一个Timer:

    private Timer timer;


    @Override
    protected void onResume (){
        super.onResume();       
        timer = new Timer();
        timer.schedule(new OpenActivityTask(), SOME_TIME_IN_MILLIS);            
    }

    @Override
    protected void  onPause (){
        super. onPause();
        timer.cancel();     
        timer = null;
    }

    private static class OpenActivityTask extends TimerTask {

        @Override
        public void run() {
            //TODO Go to new activity
        }
    }

It provides cancellation in case you manually close the activity before the delay time is reached, or if it goes to background for some reason. 如果您在达到延迟时间之前手动关闭活动,或者由于某种原因它转到后台,它会提供取消。

Another option is to use the AlarmManager and schedule a one-shot PendingIntent . 另一种选择是使用AlarmManager并安排一次性PendingIntent This approach is probably shorter in code and also allows for safe cancellation: 这种方法在代码中可能更短,并且允许安全取消:

    PendingIntent pi;

    @Override
    protected void onResume (){
        super.onResume();       

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(this, YourOtherActivity.class);
        pi = PendingIntent.getActivity(this, 0, intent, 0);

        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + SOME_TIME_IN_MILLIS, pi);           
    }

    @Override
    protected void  onPause (){
        super. onPause();

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pi);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM