Android 警报管理器 API

Android 警报管理器 API

问题描述:

我的任务是开发一个闹钟应用.一些简单的东西可以让用户在特定时间设置闹钟,并选择闹钟响起时应该播放的音乐文件.该应用程序在三星、摩托罗拉和 Nexus 等手机上运行良好,但在小米、Oppo、Vivo 手机上,闹钟不会在正确的时间响起,有时甚至根本不会响起.我使用过 Android 的 AlarmManager API.我想知道我是否还需要做其他事情才能让警报在 xiomi、oppo 和 vivo 等设备上按预期工作.* 上的大多数解决方案对我来说都没有奏效.如果此 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 * 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);
        }
    }