android - get time of the next alarm - android

Is there a way in Android to get the upcoming alarm time?
Thanks
Clarification: I am talking about the native android Alarm Clock appliction

Starting with API level 21 the constant Settings.System.NEXT_ALARM_FORMATTED is deprecated.
Instead, you should use:
AlarmManager.getNextAlarmClock()
https://developer.android.com/reference/android/app/AlarmManager.html#getNextAlarmClock()

String nextAlarm = Settings.System.getString(getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);

Needs to be:
String nextAlarm = android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.NEXT_ALARM_FORMATTED);

Related

What is the alternative for casting onALarmlistener in android API level 24 for API 21

This is my code
alarmStart = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmStart.set(AlarmManager.RTC,startTime.getTime(),"alarm",
new AlarmManager.OnAlarmListener()
{
#Override
public void onAlarm() {
//On alarm code here
}
},null);
It says that cast to OnAlarmListener requires API level 24(current min is 21).
But i want it to run on all API >=21. What changes should i make in my code?
According to the doc (emphasis mine):
Direct-notification alarms: the requester must be running continuously
from the time the alarm is set to the time it is delivered, or
delivery will fail. Only one-shot alarms can be set using this
mechanism, not repeating alarms.
IMO this is just some kind of wrapper around Messenger or the like.
You could implement similar feature by embedding a Messenger (which will execute your onAlarm code) into your PendingIntent, and call it when your PendingIntent goes off.

TimePicker getCurrentMinute, getCurrentHour method is deprecated in API 23

I'm Creating a Alarm based Application with API version 23. But TimePicker getCurrentMinute,getCurrentHour method is Deprecated. Which method will be replace of this method ?
EDIT:
According to google documentation now we have to use getMinute() and getHour() method. But it's only working in api 23. what we have to do for other api?
Just check the Android official docs .. http://developer.android.com/reference/android/widget/TimePicker.html
it tells you everything
public Integer getCurrentMinute ()
Added in API level 1
This method was deprecated in API level 23.
Use getMinute()
Returns
the current minute
and
public Integer getCurrentHour ()
Added in API level 1
This method was deprecated in API level 23.
Use getHour()
Returns
the current hour in the range (0-23)
Android offers runtime checks for API version.
Most of the time in such cases, you'll want to run cases like
if (Build.VERSION.SDK_INT >= 23 )
myTimePickerView.getHour();
else
myTimePickerView.getCurrentHour();
#see http://developer.android.com/reference/android/os/Build.VERSION.html
i faced same issue. if you write your code such as
getCurrentHour()*(striked through) it doesnt make an issue
the code runs gets complied and runs saying
"complete build performed and run since..."
since if you use getHour or getMinute you have to put the version.SDK if else statement (which i tried but didnt work at that time for some reason)
and none the less increases code lines and complexity.

How to receive time format changed 12 to 24 vice versa from native device settings?

I have a requirement to update the current time in my application as per device time format(12/24). I need any callback/listeners/receivers which will let me know that time format has been changed so that i can update time in my application accordingly.
I got the way to get current timeformat using below code snippet
String value = android.provider.Settings.System.getString(this.getContentResolver(), android.provider.Settings.System.TIME_12_24);
But looking for any callback which will let me know the same in run time.
Thanks in advance.
android.text.format.DateFormat.is24HourFormat(Context context);
Use this function to get the time format.
Register a Broadcast receiver which listens to ACTION_TIMEZONE_CHANGED,ACTION_TIME_CHANGED,ACTION_TIME_TICK.
Something like this,
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
registerReceiver(youReciverHere,intentFilter);
Do work on the receiver s onReceive() function.

Android:Changing time format as per the Device's Current time format

How to get current time format of the android device? i.e. If the android device's current time format is changed from 12hr to 24 hr format and vice versa, then how to check that setting programatically?
I want to change the time shown in my app widget to be of same format as the device's and whenever the time format is changed then my widget should also show that time format. Any pointers will be helpful.
thanks.
You can use TIME_12_24.
int time_format = 0;
try {
time_format = Settings.System.getInt(getContentResolver(), Settings.System.TIME_12_24);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
Log.i(TAG,"Time format: "+time_format);
time_format will have 12 or 24 based on the settings.
Make sure you have added this permission to manifiest file.
<user-permission android:name="android.permission.WRITE_SETTINGS" />
EDIT :
You can also use DateFormat.is24HourFormat(). It doesn't require an extra permission.
The simple way would be to use to use android native android.text.format.DateFormat class's is24HourFormat(this) static method as below.
if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) {
// 24 hour format
}
else
{
// 12 hour format
}
check, http://developer.android.com/reference/android/text/format/DateFormat.html
DateFormat::getDateFormat()
Date date = new Date(location.getTime());
dateFormat.format(date);
Please check this...
http://developer.android.com/reference/java/text/SimpleDateFormat.html
How do you format date and time in Android?
24 hour clock not working in dateformat android

Running task periodicaly(once a day/once a week)

I want to run some task (i.e. get my web site news page) periodically (once a week/ a day), even if my application is closed. Is it possible?
Yes it is, you need to look at the AlarmManager to setup a reoccurring "Alarm". This is better for battery life on the device, as unlike a service it does not run constantly in the background. The Alarm triggers a broadcast receiver which will execute your custom code.
As a final note - there are enum values for the timing of the Alarm including daily, half daily and many more although you can just set an actual value.
A good example can be found in the follow SO post:
Alarm Manager Example
Update
Newer features have been added to Android. If you are reading this then I would advise you now look into GcmNetworkManager. This optimises battery life and works pre-lollipop. For Lollipop onwards you can use JobScheduler. I would advise using these classes over the AlarmManager.
I think the best fit is GcmNetworkManager. Basically it has everything you need from AlarmManager plus persistence, so job can proceed executing after reboot.
Example:
PeriodicTask task = new PeriodicTask.Builder()
.setService(MyTaskService.class)
.setTag(TASK_TAG_PERIODIC)
.setPeriod(5L)
.build();
mGcmNetworkManager.schedule(task);
As an alternative I'm comparing the current week:
Calendar cal = Calendar.getInstance();
int currentWeekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
SharedPreferences sharedPreferences= this.getSharedPreferences("appInfo", 0);
int weekOfYear = sharedPreferences.getInt("weekOfYear", 0);
if(weekOfYear != currentWeekOfYear){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("weekOfYear", currentWeekOfYear);
editor.commit();
// Your once a week code here
}
I'm not advocating this is better than the Alarm solution. I'm just showing a different approach.

Categories

Resources