简体   繁体   English

在预设的时间后启动意图

[英]Make an intent start after a preset amount of time

I want a new activity to be started via an intent after a preset amount of time of inactivity has passed. 我希望在预设的不活动时间过后,通过intent启动新活动。 I used the code pasted below but it still does not work and it stays on the same screen. 我使用下面粘贴的代码,但它仍然不起作用,它保持在同一个屏幕上。 I know this is not the android-y way to do it, but I'm pretty sure it should work (it makes more sense to me this way). 我知道这不是android-y的方法,但我很确定它应该可行(这对我来说更有意义)。 On a side note, could I use this to make something like a textview, button or other object change its visibility from invisible to visible ? 在旁注中,我是否可以使用它来制作类似文本视图,按钮或其他对象的内容,将其可见性从不invisible变为visible I've looked around and haven't found any answers. 我环顾四周,没有找到任何答案。 Thanks in advance. 提前致谢。

public class Welcome extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(10000); 
            } catch (InterruptedException e) {
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //do stuff
                    Intent myIntent = new Intent(Welcome.this,MainActivity.class);
                    Welcome.this.startActivity(myIntent);
                    overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
                }
            });
        }
    };

You could do that with a handler : 您可以使用处理程序执行此操作:

 new Handler().postDelayed(<your runnable>, delayInMillis);

For updating widgets, it's not a very good idea to do it that way. 对于更新小部件,以这种方式执行它并不是一个好主意。 You would be better using an AsyncTask if the delay is not very large. 如果延迟不是很大,你最好使用AsyncTask

You can make a thread and make it sleep for that interval of time. 您可以创建一个线程并使其在该时间间隔内休眠。 After that time your new intent will be started. 在那之后,您的新意图将开始。

Eg:- 例如:-

Thread splashTread = new Thread() {
                   @Override
                   public void run() {
                       try {
                           int waited = 0;
                           while(_active && (waited < _splashTime)) {
                               sleep(100);
                               if(_active) {
                                   waited += 100;
                               }
                           }
                       } catch(InterruptedException e) {
                           // do nothing
                       } finally {
                           finish();

                          Intent intent = new Intent(First_Intent.this,Second_Intent.class );
                            startActivity(intent);
                       }
                   }
               };
               splashTread.start();


               }

You can try this... 你可以尝试这个......

Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
             // TODO Auto-generated method stub
             Do your thing......

             Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(intent);
        }
    }, 3000);

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

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