如何在 Android 中的预设时间后开始 activity?
How to start an activity after a preset time in Android?
我一直在尝试在用户设置的预定时间后启动 activity。
我有两项活动,一项是通过以下方式向另一项 activity 发送一些字符串:
Intent go = new Intent(MainActivity.this, CallComeActivity.class);
go.putExtra("sub", name);
go.putExtra("photo_path", fname);
go.putExtra("text", number);
startActivity(go);
我正在使用以下方式接收这些字符串:
String sub = intent.getStringExtra("sub");
String photo_path=intent.getStringExtra("photo_path");
String text = intent.getStringExtra("text");
现在我需要在一段时间后开始第二个 activity。
我尝试使用 BroadCast Receiver,但无法使用 Broadcast Receiver 将这些字符串发送到第二个 class。我只能在不传递这些字符串的情况下触发第二个 class。
请帮助。
你想做这样的事情吗
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);
你需要的是 AlarmManager.., and you can use 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);
来自文档:
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. 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.
您可以使用 AlarmManager 安排一个警报,一旦您调用了 AlarmReceiver 的 onReceive 方法,您就可以启动 activity。但它可能看起来很突然,特别是如果用户正在使用其他应用程序。
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);
尝试使用 TimerTask 在特定时间后执行某些操作。
我用它来显示 mi app 的闪屏几秒钟。
我一直在尝试在用户设置的预定时间后启动 activity。 我有两项活动,一项是通过以下方式向另一项 activity 发送一些字符串:
Intent go = new Intent(MainActivity.this, CallComeActivity.class);
go.putExtra("sub", name);
go.putExtra("photo_path", fname);
go.putExtra("text", number);
startActivity(go);
我正在使用以下方式接收这些字符串:
String sub = intent.getStringExtra("sub");
String photo_path=intent.getStringExtra("photo_path");
String text = intent.getStringExtra("text");
现在我需要在一段时间后开始第二个 activity。 我尝试使用 BroadCast Receiver,但无法使用 Broadcast Receiver 将这些字符串发送到第二个 class。我只能在不传递这些字符串的情况下触发第二个 class。 请帮助。
你想做这样的事情吗
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);
你需要的是 AlarmManager.., and you can use 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);
来自文档:
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. 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.
您可以使用 AlarmManager 安排一个警报,一旦您调用了 AlarmReceiver 的 onReceive 方法,您就可以启动 activity。但它可能看起来很突然,特别是如果用户正在使用其他应用程序。
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);
尝试使用 TimerTask 在特定时间后执行某些操作。 我用它来显示 mi app 的闪屏几秒钟。