可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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(); } }