Skip to content Skip to sidebar Skip to footer

How To Pass The Alarm Value To Alarm Receiver

I tried to develop a sample Alarm Application. I was searched Google and SC, most of the examples confusing me. I have done with my code, but why it failed to pass the alarm value

Solution 1:

Pass the values by

intent .putExtra("test", "ValueReceived");

and then in onReceive() get the value by

intent.getStringExtra("test")

Solution 2:

Try with

OnTimeSetListener ontime = new OnTimeSetListener() 
{

    publicvoid onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        txt_time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));

        Intent intent = new Intent(getActivity(), AlarmReceiver.class);

        intent.putExtra("time_value",String.valueOf(hourOfDay) + " : " + String.valueOf(minute));

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, txt_time.getTimeInMillis(), pendingIntent);
    }
};

Solution 3:

You need to convert the selected time in long value (milliseconds)

use the below code to do this.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.AM_PM, Calendar.PM); // set AM or PMlong timeInMillis = calendar.getTimeInMillis();

and then pass this value timeInMillis in

alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

Post a Comment for "How To Pass The Alarm Value To Alarm Receiver"