简体   繁体   中英

Does not switch to another activity with following code in android

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);

Is this code correct?

Example from 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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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