NEXT_ALARM_FORMATTED is deprecated

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

Since API 21 / Android 5.0

The field Settings.System.NEXT_ALARM_FORMATTED is deprecated

What is the alternative for doing this? I saw http://developer.android.com/reference/android/app/AlarmManager.html#getNextAlarmClock() But I really don't know how to implement it.

回答1:

You should upgrade android to api level 21 and your device also should be compatible with that, you can remove try..catch block also but it is better if in your manifest.xml android:minSdkVersion is less than 21.

try {     AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);     am.getNextAlarmClock();     Log.d("Nextalarm",  am.getNextAlarmClock().toString());     } catch (NoSuchMethodError e) {         e.printStackTrace();     } 


回答2:

getNextAlarmClock() is a public method in AlarmManager class which was introduced in API level 21. You need to install API level 21 in order to use this method. Also, make necessary changes in Android Project Build Target.



回答3:

I needed some code to simply test if any alarms exist, so I can update a graphic on a full screen app. Thank you to everyone who contributed. Based on all that, here is what I ended up implementing:

private void testAlarms() {     String nextAlarm = null;     if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {         AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);         am.getNextAlarmClock();         try {             nextAlarm = am.getNextAlarmClock().toString();         } catch (Exception e) {             e.printStackTrace();         }     } else {         nextAlarm = Settings.System.getString(getContentResolver(),Settings.System.NEXT_ALARM_FORMATTED);     }     if(TextUtils.isEmpty(nextAlarm)) {         hideAlarm();     } else {         showAlarm();     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!