度多个的PendingIntent使用AlarmManager同

度多个的PendingIntent使用AlarmManager同

本文介绍了调度多个的PendingIntent使用AlarmManager同一活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我注意到奇怪的现象,当我试图计划使用AlarmManager在未来的运行活动。看code以下时,第一活性在20秒开始,而第二活性没有在40秒内,而不是它是仅60秒后开始启动。任何人都可以解释为什么第二个意图不安排第二个活动,而不是被称为第三意图呢。这是否意味着我只能有一个意图在AlarmManager的活动。

  //悬而未决上午意图
    意图myIntent1 =新意图(这一点,Activity1.class);
    pendingIntent1 = PendingIntent.getActivity(在此,0,myIntent1,0);
    AlarmManager alarmManager1 =(AlarmManager)getSystemService(ALARM_SERVICE);
    日历CALENDAR1 = Calendar.getInstance();
    //calendar1.set(Calendar.YEAR,的Calendar.MONTH + 1,Calendar.DAY_OF_MONTH,morningTime,0,0);
    calendar1.setTimeInMillis(System.currentTimeMillis的());
    calendar1.add(Calendar.SECOND,20);
    alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingIntent1);    //中午未决意图
    意图myIntent2 =新意图(这一点,Activity2.class);
    pendingIntent2 = PendingIntent.getActivity(在此,0,myIntent2,0);
    AlarmManager alarmManager2 =(AlarmManager)getSystemService(ALARM_SERVICE);
    日历CALENDAR2 = Calendar.getInstance();
    //calendar2.set(Calendar.YEAR,的Calendar.MONTH + 1,Calendar.DAY_OF_MONTH,中午,0,0);
    calendar2.setTimeInMillis(System.currentTimeMillis的());
    calendar2.add(Calendar.SECOND,40);
    alarmManager2.set(AlarmManager.RTC_WAKEUP,calendar2.getTimeInMillis(),pendingIntent2);    //夜间挂起的意图
    意图myIntent =新意图(这一点,Activity2.class);
    的PendingIntent = PendingIntent.getActivity(在此,0,myIntent,0);
    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
    台历挂历= Calendar.getInstance();
    //calendar.set(Calendar.YEAR,的Calendar.MONTH + 1,Calendar.DAY_OF_MONTH,夜间,0,0);
    calendar.setTimeInMillis(System.currentTimeMillis的());
    calendar.add(Calendar.SECOND,60);
    alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),的PendingIntent);'


解决方案

No, but it means that you need distinct PendingIntents. You are calling:

Intent myIntent = new Intent(this, Activity2.class);
pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);

twice, and therefore the two getActivity() calls are returning the same PendingIntent.

Either:

  • Use a different value for the 2nd parameter to getActivity(), or

  • Do something to the Intent objects to make them sufficiently different, such as using different action strings (note: extras are not sufficient)

这篇关于调度多个的PendingIntent使用AlarmManager同一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:02