Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 07);
calendar.set(Calendar.DAY_OF_MONTH, 07);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.HOUR, 23);
calendar.set(Calendar.MINUTE, 52);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
intent.setClass(this, MyNotificationService.class);
PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
这段代码是否正确?
答案 0 :(得分:0)
来自here
的示例Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class); //MyScheduledReceiver extends from BroadcastReciever
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
== MyScheduledReceiver.java ==
public class MyScheduledReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}