Vigbyor
Vigbyor

Reputation: 2604

Only Second Alarm is executing why?

I have two AlarmManager in my activity as follows,

public class MainActivity extends Activity 
{
    private AlarmManager alarm1,alarm2;  
    private Intent intent1,intent2; 
    private PendingIntent pi1,pi2;

    private int HOUR=15,MINUTE=46,SECOND=10;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = new GregorianCalendar();
        calendar.setTimeZone(TimeZone.getDefault());
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, HOUR );
        calendar.set(Calendar.MINUTE, MINUTE );
        calendar.set(Calendar.SECOND, SECOND );

        intent1 = new Intent ( MainActivity.this, BroadCastAlarm.class );
        pi1 = PendingIntent.getService( MainActivity.this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT );
        alarm1 = (AlarmManager) getSystemService( ALARM_SERVICE );
        alarm1.set( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi1 );

        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTimeZone(TimeZone.getDefault());
        calendar1.setTimeInMillis(System.currentTimeMillis());
        calendar1.set(Calendar.HOUR_OF_DAY, HOUR );
        calendar1.set(Calendar.MINUTE, MINUTE );
        calendar1.set(Calendar.SECOND, SECOND+30 );

        intent2 = new Intent ( MainActivity.this, BroadCastAlarm.class );
        pi2 = PendingIntent.getService( MainActivity.this, 1, intent2, PendingIntent.FLAG_CANCEL_CURRENT );
        alarm2 = (AlarmManager) getSystemService( ALARM_SERVICE );
        alarm2.set( AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pi2 );
    }
}

BroadCastAlarm.java is only a service that display Toast message. Here whenever I am executing above code only second Alarm is getting execute. How come first Alarm is not getting executed ?

Upvotes: 2

Views: 341

Answers (3)

Alex Cabrera
Alex Cabrera

Reputation: 373

As other answers suggest, the second alarm is overriding the first one as they share the same action. If you want to diferentiate alarms, and not just overriding your previous ones you must set an action.

first of all, you need to add this in your application's manifest tag:

<receiver android:name="com.yourcompany.yourpackage.AlarmReceiver" android:process=":remote" >
    <intent-filter>
        <data android:scheme="timer:" />
    </intent-filter>
</receiver>

When defining your intents, just add something like:

intent1.setData(Uri.parse("timer:intent1"));
...
intent2.setData(Uri.parse("timer:intent2"));

Then you just need to define a BroadcastReceiver class to do stuff. If you want a toast:

public class AlarmReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "I'm an alarm toast!!", Toast.LENGTH_LONG).show();
   }
}

I also suggest you to change your pending intent flags to: PendingIntent.FLAG_UPDATE_CURRENT.

Upvotes: 1

njzk2
njzk2

Reputation: 39397

The PendingIntents represent the same Intent, hence your alarms are identical, hence your second alarm replaces the first one.

From the documentation,

If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

Also,

requestCode Private request code for the sender (currently not used).

(that the value you changed from 0 to 1)

Upvotes: 1

Pradip
Pradip

Reputation: 3177

intent of alarm1 is replaced by the intent of alarm2. This is basic reason why only second alarm is working.

Upvotes: 0

Related Questions