如何在用户指定的时间内启动我的应用程序。 让我们说开始时间是早上8点,结束时间是下午4点 我将开始时间和结束时间的值存储在共享首选项中。
public static synchronized void scheduleTimeoutCheck(final int time) {
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, time / 100);
calendar.set(Calendar.MINUTE, time % 100);
calendar.set(Calendar.SECOND, 1); // Add 1 second of additional delay
if (calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis() <= 0) {
// Time has already past, schedule for next day
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
new Timer(true).schedule(new TimerTask() {
@Override
public void run() {
// "its time for scheduleTimeoutCheck
/*if (Utility.checkSystemTimeout())
{*/
// Re - schedule this for the next day //
Utility.checkLockdownAppSchedule();
// }
}
}, calendar.getTime());
System.out.println("Timeout Check is scheduled to run at " + calendar.getTime().toString());
}
答案 0 :(得分:0)
启动应用程序意味着您想要在准确的时间开始活动?
我认为这不是一个好的用户案例。但是对于此任务,您必须将AlarmManager, plus a Service
与您的逻辑一起使用(服务对用户不可见)。
如果您想要通知用户您可以使用NotificationManager。或者在最后一种情况下,您可以使用对话框启动半透明活动。请注意,要从服务启动活动,您应该设置key
SINGLE_TASK
答案 1 :(得分:0)
你要做的事情已经完整描述here
首先在某个地方安排您的时间,例如onCreate()
:
//create new calendar instance for your start time
Calendar startTime= Calendar.getInstance();
//set the time to 4AM or your USER SPECIFIED start time
startTime.set(Calendar.HOUR_OF_DAY, 4);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);
//create a pending intent to be called at 4AM
PendingIntent startPI = PendingIntent.getService(yourActivity.this, 0, new Intent("startApplicationReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, startPI);
//----------------------------------------------------
//create new calendar instance for your end time
Calendar endCalender = Calendar.getInstance();
//Set the time to 8PM or your USER SPECIFIED end time
endCalender.set(Calendar.HOUR_OF_DAY, 8);
endCalender.set(Calendar.MINUTE, 0);
endCalender.set(Calendar.SECOND, 0);
//create a pending intent to be called at 8 AM
PendingIntent endPI= PendingIntent.getService(yourActivity.this, 0, new Intent("endApplicationReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, endCalender.getTimeInMillis(), AlarmManager.INTERVAL_DAY, endPI);
添加警报类:
开始时间接收器:
public class startApplicationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try { //Start your application
Intent intent = new Intent();
intent.setClassName("package.name", "<your_package_name>");
startActivity(intent);
} catch (NameNotFoundException e) { //You don't have the application installed
Log.e(TAG, e.getMessage());
}
//!!!If you WANT to repeat the start application alarm EVERY day
//Comment/Remove the next 4 lines
//Cancel start time alarm
Intent intent = new Intent(yourActivity.this, startApplicationReceiver.class);
PendingIntent startPI = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(startPI);
}
}
结束时间接收器
public class endApplicationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//finish your application
finish();
//!!!If you WANT to repeat the endapplication alarm EVERY day
//Comment/Remove the next 4 lines
//Cancel end time alarm
Intent intent = new Intent(yourActivity.this, endApplicationReceiver.class);
PendingIntent endPI = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(endPI);
}
}
你需要做的最后一件事就是在你的
中注册你的接收器<强>的Manifest.xml:强>
<receiver android:name="startAlarm" >
<intent-filter>
<action android:name="startApplicationReceiver" >
</action>
</intent-filter>
</receiver>
<receiver android:name="endAlarm" >
<intent-filter>
<action android:name="endApplicationReceiver" >
</action>
</intent-filter>
</receiver>
这应该可以帮到你。
注意:您必须至少手动启动一次应用程序,才能注册警报接收器。
答案 2 :(得分:0)
您可以使用 AlarmManager 来实现此类功能。
要充分了解如何实施它,you can study this good article.和this one by Java Code Geeks.