I want to set the alram for phone through program coding. I have an button where by clicking DatePicker come & also time picker. I want what time i set here that time should be set in phones alaram. How can I do that . plz give me the answer . Thank you
Please try this....
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 0);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2);
Intent intent1 = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.cancel(sender1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Alarm a = new Alarm ();
a.setAlarm();
b1.setText(prod);
}
});
Related
I am using Alarm Manager to schedule different jobs to be executed at different hours of the day. When i am scheduling only one job it is working expectedly, but when i schedule multiple jobs, only the last scheduled gets executed. Below are my codes.
private void triggerAlarm(int hr, int min, String sid, String cid) {
Intent intentToFire = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
intentToFire.putExtra("cid", cid);
intentToFire.putExtra("sid", sid);
intentToFire.setAction(ACTION_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intentToFire, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().
getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hr);
c.set(Calendar.MINUTE, min);
c.set(Calendar.SECOND, 0);
long afterTwoMinutes = c.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, afterTwoMinutes, alarmIntent);
//sendBroadcast(intentToFire);
}
You can use id for your AlarmReceiver I Also Using this and hope it's also works for you
in targetCal Param just pass your calander obje where you selecting time for alarm
like this
private void setAlarm(Calendar targetCal) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
appIntent);
}
Hope it works for you.
How do i use the two vales i have (hour and minute ) to set and alarm at that time? The values i have unfortuntly arent formated so if hour is > 12 it wont go to one (Its in milatary time format) .Did i set up the pending intent correctly?
public void setUpAlarm(int hour,int minute) {
long newTime;
Intent alarm = Intent(setAlarmList.this, AlarmReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent waiting = PendingIntent.getBroadcast(setAlarmList.this, 0, alarm, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, hour.);
use below code
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, h); // h->hour in 24 hrs system
cal.set(Calendar.MINUTE, m); // m->minutes
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT>16)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
} //alarmIntent is your PendingIntent
read this documentation
I am setting the alarm in one activity, and want to cancel it in another class which is a service. Following is my code:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
And following is where i am deleting the alarm (where a is pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));
in my application I have set the notification inside onCreate() of launcher activity. I am getting result successfully but as it is written inside the onCreate it is showing notification every time I launch app also.
How should I resolve this, I want the notification only on given time.
Below is the code I am using :-
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 49);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(Main.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Create a new method and call the method when you want your notification to launch.
public void showNotification() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 49);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(Main.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
Now when you want to call this method simple write:
showNotification();
Hope this helps.
I'm having an issue with my AlarmManger:
The following correctly adds one day (because the time 9:25 has passed) the output is correct yet the AlarmManager calls the Broadcast within a couple seconds. Here's the relevant snippit. Any ideas?
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, NotificationReceiver.class);
NotificationReceiver instance = new NotificationReceiver();
intent.setAction(instance.NOTIFICATION_SEND);
intent.putExtra("TYPE",instance.NOTIFICATION_TYPE_PROGRESS);
intent.putExtra("Ticker","Waiting for arrival.");
intent.putExtra("ID",1);
intent.putExtra("title","Check-In");
intent.putExtra("message","A message");
if(System.currentTimeMillis()>cal.getTimeInMillis()){
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
cal.add(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
Log.d("ALARMS","Set for monday.");
}
else{
cal.add(Calendar.DATE,1);
Log.d("ALARMS","Set for tomorrow. "+cal.get(Calendar.DAY_OF_MONTH));
}
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24332, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),4000,pendingIntent);
Resolved:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24, intent, PendingIntent.FLAG_UPDATE_CURRENT);