我的任务是开发一个闹钟应用程序。这是一个简单的功能,可以让用户在特定时间设置闹钟,并选择在闹钟响起时播放的音乐文件。该应用程序在三星、摩托罗拉和Nexus等手机上运行得很好,但在小米、Oppo和Vivo手机上,闹钟不会在正确的时间响起,有时甚至根本不会响起。我已经使用过安卓的AlarmManager接口。我想知道是否还需要做些什么才能让闹钟在xiomi、oppo和vivo等设备上像预期的那样工作。StackOverflow上的大多数解决方案都不适合我。如果在此子subreddit中有开发人员以前使用过AlarmManager API或开发过具有警报功能的应用程序,id真的很感激能更深入地了解AlarmManager API,以及为什么它们在所有手机上都不能像预期的那样工作,以及是否有任何我应该使用的替代方案。
发布于 2018-03-01 11:39:44
Bellow是一种对你有帮助的方法,我用它工作的不同设备进行了测试。
/**
* Method for start Alarm on Defined time
*
* @param context
*/
public static void startAlarm(Context context,int minutes) {
Logger.print("AlarmReceiver startAlarm called");
Intent alarmIntent = new Intent(context, WakefulBrodcastReceiverService.class);
alarmIntent.setAction("testAPP");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123451, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
long alarmPeriodicTime = System.currentTimeMillis() + Utils.getTimeInMilliSec(Constant.TimeType.MINUTE, minutes);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
manager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else {
manager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
}
}https://stackoverflow.com/questions/49042187
复制相似问题