简体   繁体   English

按下android中的按钮后如何启动一个不同的活动?

[英]How to start a different activity with some delay after pressing a button in android?

I want that a new activity should start with some delay on pressing a button. 我希望新的活动开始时按下按钮会有一些延迟。 Is it possible to do that , and whats the procedure. 是否有可能这样做,并且该程序是什么。

Use a postDelayed() call with a runnable that launches your activity. 使用postDelayed()调用runnable启动您的活动。 An example code could be 示例代码可以是

    //will care for all posts
    Handler mHandler = new Handler();

    //the button's onclick method
    onClick(...)
    {
        mHandler.postDelayed(mLaunchTask,MYDELAYTIME);
    }

    //will launch the activity
    private Runnable mLaunchTask = new Runnable() {
        public void run() {
            Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS);
            startActivity(i);
        }
     };

Note that this lets the interface remain reactive. 请注意,这使接口保持反应。 You should then care for removing the onclick listener from your button. 然后,您应该关注从按钮中删除onclick侦听器。

You could call a Runnable using the Handler postDelayed() method. 您可以使用Handler postDelayed()方法调用Runnable。

Here's an example (http://developer.android.com/resources/articles/timed-ui-updates.html): 这是一个例子(http://developer.android.com/resources/articles/timed-ui-updates.html):

private Handler mHandler = new Handler();

...

OnClickListener mStartListener = new OnClickListener() {
   public void onClick(View v) {
            mHandler.postDelayed(mUpdateTimeTask, 100);
   }
};

private Runnable mUpdateTimeTask = new Runnable() {
   public void run() {
       // do what you need to do here after the delay
   }
};

Props to @mad for getting it right the first time around. 推荐给@mad,让他们第一次做对。

Use This code 使用此代码

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            final Intent mainIntent = new Intent(CurrentActivity.this, SecondActivity.class);
            LaunchActivity.this.startActivity(mainIntent);
            LaunchActivity.this.finish();
        }
    }, 4000);

您可以使用该方法postDelayed(Runnable action, long delayMillis)View到添加Runnable到消息队列的(近似)的延迟后运行。

Sometimes, u need to do it whenever your app process is killed or not. 有时候,无论何时你的应用程序进程被杀,你都需要这样做。 In that case you can not use handling runnable or messages inside your process. 在这种情况下,您无法在流程中使用处理runnable或消息。 In this case your can just use AlarmManager to this. 在这种情况下,你可以使用AlarmManager。 Hope this example helps anybody: 希望这个例子可以帮助任何人:

Intent intent = new Intent();
...

PendingIntent pendingIntent = PendingIntent.getActivity(<your context>, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, <your delay>, pendingIntent);
runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                }
            }, 4000);
        }
    });

try this piece of code 试试这段代码

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {

        // run AsyncTask here.    


    }
 }, 3000);

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

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