AlarmManager
或WakefulBroadcaastReceiver
(我真的不知道它是哪一个)当app在后台并且屏幕被锁定时不起作用。我可以通过代码或设备的选项以某种方式修复它吗?
我还要补充说,我正在测试它并且无法正常工作的手机是Huwaei P8 Lite和Android 5.0.1
原始问题:
我在我的应用中使用AlarmManager
以某些时间间隔显示通知。当应用程序打开或应用程序处于后台时(例如,在我按下主页按钮后),它可以正常工作。当应用程序处于活动状态时锁定手机屏幕时,它也有效。但是,当我的应用处于后台时,我锁定手机时,BroadcastReceiver
未调用AlarmManager
。这是为什么?问题是在AlarmManager中,还是在我的接收器中?
此外,我还有一个红利问题:在应用程序被杀后,AlarmManager中的警报是否可以正常工作?
这是我的代码:
public class NotificationService extends IntentService {
public NotificationService(String name) {
super(name);
}
public NotificationService() {
super("NotificationService");
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this).setContentTitle("TEXT")
.setContentText("TEXT")
.setLights(Color.GREEN,1000,500)
.setSmallIcon(R.drawable.ic_event_white_24dp).getNotification();
notificationManager.notify(0,notification);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(this,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this,0,i,0);
long nextAlarm = System.currentTimeInMillis()+1000*60;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(nextAlarm, pi);
alarmManager.setAlarmClock(alarmClockInfo, pi);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, nextAlarm, pi);
}else {
alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, nextAlarm, pi);
}
AlarmReceiver.completeWakefulIntent(intent);
}
接收器:
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("test","onReceive started");
Intent i = new Intent(context,NotificationService.class);
startWakefulService(context,i);
}
}
当屏幕被锁定且app在后台时,接收器永远不会启动,因为我看不到行:Log.e("test","onReceive started");
已执行
我在清单
中有WAKE_LOCK权限答案 0 :(得分:0)
答案 1 :(得分:0)
您必须考虑添加:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
在通知IntentService上显示和添加功能的权限,以验证您的通知是否已被触发。
所以你可以使用JobScheduler服务:
JobScheduler jobScheduler =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (!enabled) {
jobScheduler.cancel(JOB_ID);
Log.d(TAG, "cancelling scheduled job");
} else {
long interval = AlarmManager.INTERVAL_DAY;
JobInfo job = new JobInfo.Builder(JOB_ID,
new ComponentName(getPackageName(),
ScheduledJobService.class.getName()))
.setPersisted(true)
.setPeriodic(interval)
.build();
jobScheduler.schedule(job);
Log.d(TAG, "setting scheduled job for: " + interval);
和
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class ScheduledJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
startService(new Intent(this, NotificationService.class));
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}