Android 安卓通知不';屏幕关闭时不工作
应用程序非常简单。我有一个定期的应用程序与所有的活动等 我有一个在应用程序第一次启动和/或设备重新启动时启动的服务 我有一个BroadcastReceiver设置来处理通知意图,它非常基本,功能完美 当安卓设备上的显示器打开时,通知工作正常。但当我关闭屏幕时,通知将不再创建 我是新来的通知,所以这可能是我忽略的一些简单的事情,但我似乎找不到它 在我的服务中,我构造了一个工作线程,其中包含一个计时器,该计时器每分钟触发一次,并将TimerTask作为schedule方法的一部分:Android 安卓通知不';屏幕关闭时不工作,android,timer,notifications,Android,Timer,Notifications,应用程序非常简单。我有一个定期的应用程序与所有的活动等 我有一个在应用程序第一次启动和/或设备重新启动时启动的服务 我有一个BroadcastReceiver设置来处理通知意图,它非常基本,功能完美 当安卓设备上的显示器打开时,通知工作正常。但当我关闭屏幕时,通知将不再创建 我是新来的通知,所以这可能是我忽略的一些简单的事情,但我似乎找不到它 在我的服务中,我构造了一个工作线程,其中包含一个计时器,该计时器每分钟触发一次,并将TimerTask作为schedule方法的一部分: @Overri
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
boolean isStopping = false;
if (intent != null) // if the system has to shut us down, go down gracefully
{
String intentAction = intent.getStringExtra(AppData.BROADCAST_ACTION);
if (intentAction != null && intentAction.equals("android.intent.action.REBOOT"))
{
boolean isRebooting = intent.getBooleanExtra(AppData.SYSTEM_ACTION, false);
if (isRebooting)
{
isStopping = true;
stopSelf();
}
}
}
if (!isStopping && !_workerThread.isRunning())
{
_workerThread.run();
}
return Service.START_STICKY;
}
_timer.schedule(_service.getWorker(), startDelay, executionDelay);
NotificationTask(TimerTask)最终每分钟都会从计时器中触发一次。然后,它检查一些逻辑以查看是否需要创建任何通知,如果需要,它将创建并发送如下通知:
Notification notification = new NotificationCompat.Builder(_context)
.setContentTitle(getString(R.string.notification))
.setContentText(message)
.setLargeIcon(decodeResource(_context.getResources(), iconId))
.setSmallIcon(R.drawable.logo_mark)
.setTicker(ticker)
.setAutoCancel(true)
.setSound(getRingtone(ringtoneUrl))
.setVibrate(VIBRATION_PATTERN)
.setLights(0xfff89829, 300, 5000)
.setContentIntent(intent)
.build();
_notificationManager.notify(notificationId, notification);
根据通知的类型,这些通知是唯一的,我不在乎用户在我覆盖下一个通知之前是否响应了一个通知。我的问题是,如果屏幕关闭,我不会收到通知
请帮忙
更新 我想添加代码,以防有人像我一样陷入困境 让我们从AndroidManifest开始。包括2个广播接收器和您的服务
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".services.NotificationService"/>
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".receivers.NotificationReceiver" />
主接收器将启动您的服务-您的服务应该扩展IntentService而不是常规服务。这是主接收器:
context.startService(new Intent(context, NotificationService.class));
服务呢
public class NotificationService extends IntentService
{
public NotificationService()
{
super("Notification Service");
}
@Override
public void onHandleIntent(Intent intent)
{
// this is where I created my notifications
}
}
最后一个小技巧是在应用程序中再次创建AlarmManager。这将替换引导接收器中创建的AlarmManager。请注意pendingent.FLAG\u CANCEL\u CURRENT。这是在用户启动应用程序时完成的,如果AlarmManager尚未在设备引导上注册,则会注册它
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, Utils.getNow(), 60 * 1000, pendingIntent);
希望这有帮助。不要说你做错了,因为我对计时器和计时器任务没有太多经验,但我认为这是安排将来在屏幕关闭等情况下运行的标准方式。设备可能有问题,你在运行什么设备?两个设备,但果冻豆。。。三星Galaxy Nexus和Nexus 7。我曾考虑修改代码以使用AlarmManager,但我更熟悉计时器。你知道这是不是我的问题吗?我以为通知就是通知,但唉,我不知道。我将阅读更多关于AlarmManager的内容。在进一步回顾之后,Android似乎会暂停这些线程以节省电池。我将修改代码以使用AlarmManager。如果/当这起作用时,我会将此标记为正确答案。是的,我所说的是问题不在于您的通知,而在于它们不是一开始创建的。希望这能帮上忙。原来你不需要这样的服务。设置两个广播接收机最简单。一个用于设备引导,另一个用于引导接收器调用以及应用程序调用。我发现这有一个链接到一个项目,显示它相当不错
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, Utils.getNow(), 60 * 1000, pendingIntent);