当设备睡眠几个小时(棉花糖)时,闹钟不响



我创建了一个简单的闹钟,它工作得很好,在用户选择的特定时间显示通知作为简单的提醒(如果设备未醒,它会播放通知声音)。

但是当我用它作为早上叫醒的闹钟时,它不能播放通知声音。

这很难测试,因为设备必须锁定几个小时。我想这和瞌睡有关,但我不确定。此外,如果设备在夜间充电,它会播放警报。

早上手动唤醒设备后,收到通知…

报警设置代码:

        // Create an intent that will be wrapped in PendingIntent
        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        intent.putExtra("id",id);
        // Create the pending intent and wrap our intent
        PendingIntent pendingIntent =
                PendingIntent.getBroadcast(getApplicationContext(),(int)id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //get the alarm manager
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= 19)
        {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
        }
        else if (android.os.Build.VERSION.SDK_INT >= 23)
        {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent);
        }
        else alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent);

        //go back to the main activity
        onBackPressed();
我AlarmReceiver

:

public class AlarmReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
    Log.e("ALARMRECEIVER","ONRECEIVE");
    //Create a notification
    long notificationId = intent.getLongExtra("id", -1);
    if(notificationId == -1)
    {
        Log.e("AlarmReceiver","id went missing");
    }
    else
    {
        NotificationRepository repository = NotificationRepository.getInstance(context);
        Notification notification = repository.getNotification(notificationId);

        if(notification != null)
        {
            String[] icons = context.getResources().getStringArray(R.array.icons);
            int iconId = context.getResources().getIdentifier(context.getPackageName()
                    + ":drawable/" + icons[notification.getIconIndex()], null, null);
            //create the android notification
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(iconId)
                    .setContentTitle(notification.getTitle())
                    .setContentText(notification.getSubtitle())
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary));
            if(notification.isPlaySound())
            {
                mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                Log.e("ALARMRECEIVER", "SOUND");
            }
            else Log.e("ALARMRECEIVER","NO SOUND");
            if (notification.isVibrate())
            {
                mBuilder.setVibrate(new long[]{1000, 1000});
            }
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify((int) notificationId, mBuilder.build());
            //Delete the notification from the database
            repository.removeNotification(notificationId);
            Intent i = new Intent("dvanack.gmail.com.NOTIFY");
            context.sendBroadcast(i);
            AlarmReceiver.completeWakefulIntent(intent);
            Log.w("ONRECEIVE","ENDED");
        }
    }
}

我想你是棉花糖和以上的问题。

下面的代码似乎有问题:

 if (android.os.Build.VERSION.SDK_INT >= 19)
    {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
    }
    else if (android.os.Build.VERSION.SDK_INT >= 23)
    {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent);
    }

这里……"else if"不可达,因为如果android版本是棉花糖,它将再次进入第一个if条件,因为版本是>=19。希望这能解决问题。

所以条件应该是:

if (android.os.Build.VERSION.SDK_INT >= 23)
{
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime() , pendingIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19)
{
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}

最新更新