Android 警报管理器 API

编程入门 行业动态 更新时间:2024-10-23 07:39:38
本文介绍了Android 警报管理器 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的任务是开发一个闹钟应用.一些简单的东西可以让用户在特定时间设置闹钟,并选择闹钟响起时应该播放的音乐文件.该应用程序在三星、摩托罗拉和 Nexus 等手机上运行良好,但在小米、Oppo、Vivo 手机上,闹钟不会在正确的时间响起,有时甚至根本不会响起.我使用过 Android 的 AlarmManager API.我想知道我是否还需要做其他事情才能让警报在 xiomi、oppo 和 vivo 等设备上按预期工作.StackOverflow 上的大多数解决方案对我来说都没有奏效.如果此 subreddit 中的开发人员之前曾使用过 AlarmManager API 或使用过具有警报功能的应用程序,id 真的很感谢您对 AlarmManager API 的更多了解以及为什么它们无法在所有手机上按预期工作以及是否有任何替代方案我应该在用.

I had been tasked with developing an alarm app. Something simple which lets users set an alarm at a specific time and select a music file that should play when the alarm goes off. The app works great on phone like Samsung, Motorola, and Nexus but on Xiomi, Oppo, Vivo phones the alarm does not go off at the correct times and sometimes it does not fire at all. I have used Android's AlarmManager APIs. I am wondering if there is something else I need to do to get the alarm to work as expected on devices like xiomi, oppo, and vivo. Most of the solutions on StackOverflow have not worked out for me. If there are devs here in this subreddit who have worked with AlarmManager APIs before or worked on Apps with Alarm features, id really appreciates some more insight into AlarmManager APIs and why they don't work as expected across all phones and if there are any alternatives I should be using.

推荐答案

Bellow 是一种可以帮助您的方法,我使用它正在工作的不同设备进行了测试.

Bellow is the method which will help you, Which I tested with the different-different device it's working.

/** * 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); } }

更多推荐

Android 警报管理器 API

本文发布于:2023-11-27 03:38:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   Android   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!