Android 报警管理器和通知在一段时间后不工作

Android 报警管理器和通知在一段时间后不工作,android,service,notifications,broadcastreceiver,alarmmanager,Android,Service,Notifications,Broadcastreceiver,Alarmmanager,我已经设置了一个报警管理器,当该报警启动时,我已经定义了一个通知,该通知将显示在接收器的onReceive中 我在“活动停止时”中定义了报警管理器代码,因此它仅在应用程序关闭后才开始运行 下面是onStop方法中的代码 public void onStop() { super.onStop(); // alrm manager called in on stop so that timer starts after we finish the app Intent my

我已经设置了一个报警管理器,当该报警启动时,我已经定义了一个通知,该通知将显示在接收器的onReceive中

我在“活动停止时”中定义了报警管理器代码,因此它仅在应用程序关闭后才开始运行

下面是onStop方法中的代码

public void onStop() {

    super.onStop();
    // alrm manager called in on stop so that timer starts after we finish the app
    Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+NOTIFICATIONTIME,NOTIFICATIONTIME, pendingIntent);
}
我已经取消了之前在onresume中发出的警报,这样我在活动中就不会听到通知

public void onResume() {

    super.onResume();

    //this helps to cancel all the previously made alarms so that no notification in between game
    cancelAlarm(getApplicationContext());
}
public void cancelAlarm(Context context) {

    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}
这是发出通知的报警接收器

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // When our Alaram time is triggered , this method will be excuted (onReceive)
        // We're invoking a service in this method which shows Notification to the User

        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Uri notificationbark = Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.notificationbark);
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle("Name").setContentText("text").setSound(notificationbark).setAutoCancel(true).setWhen(when).setContentIntent(pendingIntent);
        notificationManager.notify(0, mNotifyBuilder.build());
    }
}

通知可能显示了一天,但现在没有

你确定在你的设备内的节能选项下,你的应用程序受到保护吗?这是新的。如何检查。虽然我没有检查您的建议,但我认为这不是问题,因为我收到了其他事情的通知。问题也不在我的设备上,但它也存在于其他设备上。伙计们,请帮助我,我已经等待了数天和数小时的答案。您可以在设置中找到能量选项。设置节能-详细信息-屏幕关闭:执行(可能因手机而异)。你必须在那里启用你的应用程序。一旦屏幕进入睡眠状态,如果没有保护,您的应用程序将不再执行。这只是一种可能性。。。