简体   繁体   English

在Android中经过预设时间后如何开始活动?

[英]How to start an activity after a preset time in Android?

I have been trying with starting an activity after a pre determined time set by the user. 我一直在尝试在用户设置的预定时间后开始活动。 I have two activities, one is sending some strings to another activity by means of: 我有两个活动,一个是通过以下方式将字符串发送到另一个活动:

Intent go = new Intent(MainActivity.this, CallComeActivity.class);

            go.putExtra("sub", name);
            go.putExtra("photo_path", fname);
            go.putExtra("text", number);

            startActivity(go);

I am receiving these strings using: 我正在使用以下方式接收这些字符串:

    String sub = intent.getStringExtra("sub");
    String photo_path=intent.getStringExtra("photo_path");
    String text = intent.getStringExtra("text");

Now I need to start this second activity after some time. 现在,我需要一些时间才能开始第二个活动。 I have tried with BroadCast Receiver but I am not able to send these strings to the second class using Broadcast Receiver. 我曾尝试使用BroadCast Receiver,但无法使用Broadcast Receiver将这些字符串发送给第二类。 I am only able to trigger the second class without passing these strings. 我只能在不传递这些字符串的情况下触发第二类。 Please Help. 请帮忙。

What you need is AlarmManager.. , and you can use PendingIntent for putting extras 您需要的是AlarmManager .. ,您可以使用PendingIntent放置其他内容

Intent go = new Intent(MainActivity.this, CallComeActivity.class);

            go.putExtra("sub", name);
            go.putExtra("photo_path", fname);
            go.putExtra("text", number);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, go, 0);

From the documentation: 从文档中:

This class provides access to the system alarm services. 此类提供对系统警报服务的访问。 These allow you to schedule your application to be run at some point in the future. 这些使您可以计划您的应用程序在将来的某个时间运行。 When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. 警报响起时,系统会广播已为其注册的Intent,并在目标应用程序尚未运行时自动启动它。 Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted. 设备处于睡眠状态时会保留已注册的警报(如果警报在这段时间内关闭,可以选择将其唤醒),但是如果将其关闭并重新启动,则将被清除。

Are you trying to do something like this 您是否正在尝试做这样的事情

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

                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    callIntent();
                }
            }, Your_pre_determined_time);

You can schedule an Alarm using AlarmManager and once you get a call to onReceive method of your AlarmReceiver, you can start the activity. 您可以使用AlarmManager计划警报,并且一旦调用AlarmReceiver的onReceive方法,就可以开始活动。 But it might look abrupt especially if the user is using some other application. 但是它看起来可能会突然变化,特别是如果用户正在使用其他应用程序。

Intent intent = new Intent(appContext, AlarmReceiver.class);
                    intent.putExtra(key, value);
                    PendingIntent alarmIntent = PendingIntent.getBroadcast(appContext,id, intent, 0);
alarmMgr.setExact(AlarmManager.RTC, remindAtTimeStamp, alarmIntent);
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        Intent go = new Intent(MainActivity.this, CallComeActivity.class);

        go.putExtra("sub", name);
        go.putExtra("photo_path", fname);
        go.putExtra("text", number);

        startActivity(go);
    }
};

Timer timer = new Timer();
timer.schedule(task, 3000);

Try using TimerTask for execute something after a certain time. 尝试在特定时间后使用TimerTask执行某些操作。 I use this for show a splash screen for mi app for tree seconds. 我用它来显示mi应用程序的启动屏幕达几秒钟。

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

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