【发布时间】:2018-11-28 16:45:56
【问题描述】:
我想使用 AlarmManager 启动通知服务。但是,当在最近的任务层中滑动应用程序时,它总是不起作用。我还使用 Manifest 中的适当条目尝试了 JobIntentService 和 JobService,一切都导致了同样的问题:通知或 startActivity() 在我关闭后没有被调用(有时!,也许是在打瞌睡模式下?)应用程序。
现在,我想也许可以回到 AlarmManager,因为它似乎对这里的其他人有用。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);
if(Build.VERSION.SDK_INT <= 23)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeForAlarm, pendingIntent);//Alarm with setExact and AlertReceiver should be better than the old Alarm Manager
}
else
{
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeForAlarm, pendingIntent);//Alarm with setExact and AlertReceiver should be better than the old Alarm Manager
}
}
else
{
final AlarmManager alarm = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), Service_Notification.class);
PendingIntent startPendingIntent = PendingIntent.getService(getActivity(), 0, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, timeForAlarm, startPendingIntent);
}
这是我用于 KitKat 以上版本的 AlertReceiver(我也尝试过扩展 BroadCastReceiver)
public class AlertReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
context.startService(new Intent(context, Service_Notification.class));
}
}
那么,我现在能做什么?提前谢谢你
【问题讨论】:
标签: android alarmmanager android-notifications android-8.0-oreo android-8.1-oreo