Disabling default AlarmClock alarm - android

I've set an alarm in the default AlarmClock app. How to disable that previously set alarm? I didn't use AlarmManager for this.
Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);
int hour = Integer.parseInt(message.substring(11,13));
int minutes = Integer.parseInt(message.substring(14,16));
if((message.substring(17,19).equalsIgnoreCase("AM") || message.substring(17,19).equalsIgnoreCase("PM")))
{
if(message.substring(17,19).equalsIgnoreCase("AM"))
{
alarm.putExtra(AlarmClock.EXTRA_HOUR, hour);
}
else
alarm.putExtra(AlarmClock.EXTRA_HOUR, hour+12);
alarm.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
alarm.putExtra(AlarmClock.EXTRA_MESSAGE, name+": "+message.substring(20));
}
startActivity(alarm);
In the string message, I'm taking the hour, minutes and AM/PM from the user.

I think you don't need the answer for this question until now but I think some other guys will be need...
Please try :
Intent itentChangeAlarm = new Intent("android.intent.action.ALARM_CHANGED");
alarmChanged.putExtra("alarmSet", enabled);
context.sendBroadcast(itentChangeAlarm);

Related

Android multiple alarms not working

public static boolean setupAlarm(String flightName, long columnId, int time,int requestCode, Context context) {
AlarmManager alarmManager;
FlightTimeObject timeObject = DataCheckingUtils.getConvertedTime(time);
try {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context.getApplicationContext(), ProcessAlarmReceiver.class);
intent.putExtra(IntentActions.INTENT_REQUEST_CODE, requestCode);
intent.putExtra(IntentActions.INTENT_SEND_STRING_FLIGHT, flightName);
intent.putExtra(IntentActions.INTENT_SEND_FLIGHT_COLUMN_ID, columnId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent,0);
//Get calendar instance
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//get hour
if (timeObject.getHour()!= -1) {
int hour = timeObject.getHour();
calendar.set(Calendar.HOUR_OF_DAY, hour);
}
//get minute
if (timeObject.getMinute()!=-1){
int minute = timeObject.getMinute();
calendar.set(Calendar.MINUTE, minute);
}
calendar.set(Calendar.SECOND, 0);
assert alarmManager != null;
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
pendingIntent);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
Hello everyone. The above code gets called within a for loop, underneath.
private static void setupAlarmToFlight(ArrayList<FlightObject> flightArray, Context context) {
int numberOfAlarms = 0;
int requestCode = 0;
for (int i = 0; i < flightArray.size(); i++){
FlightObject flight = flightArray.get(i);
String name = flight.getFlightName();
long flightColumnId = flight.getColumnId();
int flightActualTime = flight.getActualArrivalTime();
int scheduledTime = flight.getFlightScheduledTime();
int timeToParse = -2;
if (flightActualTime == -2){
timeToParse = scheduledTime;
}else{
timeToParse = flightActualTime;
}
boolean alarmSet = ExtractFlightUtilities.setupAlarm(
name,
flightColumnId,
timeToParse,
requestCode,
context);
if (alarmSet){
numberOfAlarms++;
requestCode++;
}
}
Intent intent = new Intent(IntentActions.ALARM_SET);
intent.putExtra(IntentActions.INTENT_SEND_INT, numberOfAlarms);
context.sendBroadcast(intent);
}
This code basically sets alarms for different flight arrival times from an arraylist that will start a service. The first alarm always fires right on time, but the rest never fire. I even stopped the service so it would just get to the receiver, but only the first fires. I also stopped the for loop at 2-3 alarms, but nothing.
I made sure that the hours and minutes are set correctly, and used another loop that would just set alarm in one minute after each but no luck.
Any help would be greatly appreciated.
EDITED:
I tried the suggestion and still not firing the alarms.
Something interesting I noticed, when setting breakpoints, the debugger gives preview values, and the first alarm that goes off okay, it's values, such as the requestCode and the Calendar values are green. All other following alarms are red.
For the first alarm.
The request code is green.
The pending intent looks all green.
The calendar value looks all green.
For all the other alarms.
The request code is red.
The pending intent request code red:
The calendar value is red
Again thank you for your time.
Since you set exact time, it might be because you did not check if the alarm time set is not passed already. E.g. consider the current date/time is 1-Jan-2018 11:30PM. If you set alarm for 01:00AM, based on your code, the alarm will be set for 1-Jan-2018 01:00AM since you used:
calendar.setTimeInMillis(System.currentTimeMillis())
which returns 1-Jan-2018 11:30PM and the you set time to 01:00AM which change the date to 1-Jan-2018 01:00AM which is passed. In this case you should add to alarm date one day. Something like this:
/**
* adjust time and date of alarm (alarms set in some previous years, will updated to today).
* if time is passed in today, date will +1
*
* #param alarmTimeCalendar time which may need adjustments
* #return adjusted time for alarm
*/
public static Calendar adjustAlarmTime(Calendar alarmTimeCalendar) {
Calendar adjustedDateCalendar = Calendar.getInstance();
int hour = alarmTimeCalendar.get(Calendar.HOUR_OF_DAY);
int minute = alarmTimeCalendar.get(Calendar.MINUTE);
adjustedDateCalendar.set(Calendar.HOUR_OF_DAY, hour);
adjustedDateCalendar.set(Calendar.MINUTE, minute);
adjustedDateCalendar.set(Calendar.SECOND, 0);
Date currentDate = adjustedDateCalendar.getTime();
Date alarmDate = adjustedDateCalendar.getTime();
long difference = alarmDate.getTime() - currentDate.getTime();
if (difference < 0) {
adjustedDateCalendar.add(Calendar.DATE, 1);
}
return adjustedDateCalendar;
}
So after trying multiple ways of approaching the problem, I solved it by setting the alarm as only RTC and not RTC_WAKEUP. Now all the alarms are firing right on time.
Thank you all.

Set AlarmManager only with TimePicker in Xamarin.Android, alarm starting instantly [duplicate]

This question already has an answer here:
Xamarin Android Alarm Manager Issue
(1 answer)
Closed 5 years ago.
I having trouble while trying to set an Exact Alarm with only a Time Picker.
I set a TimePicker with an Handler like this:
TimePicker
TPBtn.Click += delegate
{
TimePickerDialog dialog = new TimePickerDialog(this, TimeSelectedListener, DateTime.Now.Hour, DateTime.Now.Minute, true);
dialog.Show();
};
private void TimeSelectedListener(object sender, TimePickerDialog.TimeSetEventArgs e)
{
hour = e.HourOfDay;
minute = e.Minute;
DateTime dtNow = DateTime.Now;
int DayOfMonth = dtNow.Day;
int Month = dtNow.Month;
int Year = dtNow.Year;
Calendar cl = Calendar.Instance;
cl.Set(Year, Month, DayOfMonth, hour, minute, 0);
AlarmManager am = (AlarmManager)this.GetSystemService(AlarmService);
Intent intent = new Intent(this, typeof(OneShotAlarm));
intent.AddFlags(ActivityFlags.NewTask);
var source = PendingIntent.GetBroadcast(this, 0, intent, 0);
am.SetExact(AlarmType.RtcWakeup, cl.TimeInMillis, source);
Toast.MakeText(this, "Set Today: " + cl.TimeInMillis, ToastLength.Short).Show();
}
Unfortunately, when doing this the alarm is instantly played.
I also tried to use am.SetAlarmClock but I don't know how to use RTC time with it, can you help me please ?
I already read many thread like I am trying to set alarm on specific time using alarm manager but alarm intiated instantly? and Set AlarmManager from DatePicker and TimePicker
Thanks a lot.
am.SetExact(AlarmType.RtcWakeup, cl.TimeInMillis, source);
cl.TimeInMillis is called on Calendar.Instance which has not been manipulated in any way. So that means it is set to "now", hence the alarm firing immediately.
It is not exactly clear what time you want the alarm to fire, but you will need to provide the correct TimeInMillis to do so.

Android Clock App Set Alarm

I need to know if this is possible, and if it is, how I should proceed.
I'd like to make an app that will create a repeating alarm within the stock Android Clock App. It should go off at 8am each morning and should only take one button to activate within my app.
Thanks in advance
Try this:
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_HOUR, hour)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Source: https://developer.android.com/guide/components/intents-common.html
I found this way for Clock
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)// show on it
.putExtra(AlarmClock.EXTRA_HOUR, hour) //24 hours
.putExtra(AlarmClock.EXTRA_MINUTES, minutes); //not more than 60 :)
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

How to use Alarm Manager to schedule my android app to run in especial time?

I am new with android and I wanna make a alarm app that gets a time from user and make android system to run application in that time.Now I calculate the time between current and the entered time and use the following code to sleep app but doesn't work when my app close or even minimized:
start_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TimePicker tp = (TimePicker) findViewById(R.id.timepicker1);
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Integer res = min + hour*60;
Calendar c = Calendar.getInstance();
cur_hour = c.get(Calendar.HOUR_OF_DAY);
cur_min = c.get(Calendar.MINUTE);
Integer cur_res = cur_min + cur_hour*60;
if(cur_res > res)
{
res_min =1440-( cur_res - res);
}
else
{
res_min = res-cur_res;
}
res_min *= 60000;
new Handler().postDelayed(new Thread(){
#Override
public void run() {
super.run();
PlayMusic();
}
},res_min);
}
});
I searched hardly for solution and I find out that Alarm Manager is the answer but I couldn't find a proper code to work in this situation.I'm looking for a simple way to perform that.I don't know if there is a better way or not.
Thanks!
Did you try to use the AlarmManager? If you have, post the code that you've tried.
Look at the Google Developer API for classes such as these. They are fantastically documented.
http://developer.android.com/reference/android/app/AlarmManager.html
An example:
//You must set permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:process=":remote" android:name="Alarm"></receiver>
//In your code
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), milli, sec, min, pi);

How to start an alarm if the mobile is switch off

In my app, I use the AlarmManager class to set an alarm. To trigger the alarm after the mobile is rebooted I have used BroadcastReceiver. All works fine and my alarm is triggered at regular intervals. Now the problem arises in this case :
Suppose my current time is 2:30 pm and I set my alarm at 2:35 pm. After that, I switch off the mobile. After an hour when I switch on my mobile, no alarm is pop-up as the time on which the alarm is set. This is happening because the current time exceeds the time on which I set the alarm. To solve this issue what should I do. I have posted my code for setting alarm in the AlarmManager class. Please help me to solve this out
public class AlarmReceiver extends BroadcastReceiver {
#SuppressWarnings("static-access")
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent myIntent = new Intent(context, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, i, myIntent, i);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MILLISECOND, (int) Utilities.diff(NoteManager.getSingletonObject().getAlarmTime(i)));
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
}
public static long diff(Date date) {
long difference = 0;
try {
// set current time
Calendar c = Calendar.getInstance();
difference = date.getTime() - c.getTimeInMillis();
if (difference < 0) {
// if difference is -1 - means alarm time is of previous time then current
// then firstly change it to +positive and subtract form 86400000 to get exact new time to play alarm
// 86400000-Total no of milliseconds of 24hr Day
difference = difference * -1;
difference = 86400000 - difference;
}
}
catch (Exception e) {
e.printStackTrace();
}
return difference;
}
In The Manifest File
<receiver android:name=".AlarmReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
better way is to store that alarm details in database and retrieve it on boot via broadcast receiver as you are saying you implemented one. once notified remove the details from the database. this way u can track all your alarms. even you can start a Service on startup and do this operation
The Alarm app in the Android does the same, if your phone is switched off and there is Alarm to ring up, It will make your phone switch On , ring the alarm and go to sleep again.
Here is the link of source of Alarm app Git_Alarm app you can download it and see how it is doing this.
and if you are doing something else in your alarm reciever then to ring Alarm up. you can basically set alarmreciever again in the phone Boot up, here is the one answer which may help you Alarm problem if phone is switched off
Edit :- one link was broken, replaced it

Categories

Resources