NotificationManager.notify() 失败并出现 SecurityException
在 Android 2.2 上,NotificationManager.notify()
何时需要 WAKE_LOCK
权限?
我从一位用户那里收到了以下堆栈跟踪:
java.lang.SecurityException: Neither user ***** nor current process has android.permission.WAKE_LOCK.
at android.os.Parcel.readException(Parcel.java:1247)
at android.os.Parcel.readException(Parcel.java:1235)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
at android.app.NotificationManager.notify(NotificationManager.java:118)
at android.app.NotificationManager.notify(NotificationManager.java:94)
...
更新在 Scala 中调用代码:
notification = new Notification(android.R.drawable.stat_notify_sync,
"%s %s".format(statusTitle, finishedStatus), 0)
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL
notification.setLatestEventInfo(SyncService.this, statusTitle, finishedStatus,
PendingIntent.getActivity(SyncService.this, 0, new Intent(SyncService.this, classOf[MainActivity]), 0))
notificationManager.notify(NOTIFICATION, notification)
When does NotificationManager.notify()
require WAKE_LOCK
permission on Android 2.2?
I received following stack trace from one user:
java.lang.SecurityException: Neither user ***** nor current process has android.permission.WAKE_LOCK.
at android.os.Parcel.readException(Parcel.java:1247)
at android.os.Parcel.readException(Parcel.java:1235)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
at android.app.NotificationManager.notify(NotificationManager.java:118)
at android.app.NotificationManager.notify(NotificationManager.java:94)
...
UPDATE Calling code in Scala:
notification = new Notification(android.R.drawable.stat_notify_sync,
"%s %s".format(statusTitle, finishedStatus), 0)
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL
notification.setLatestEventInfo(SyncService.this, statusTitle, finishedStatus,
PendingIntent.getActivity(SyncService.this, 0, new Intent(SyncService.this, classOf[MainActivity]), 0))
notificationManager.notify(NOTIFICATION, notification)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎通知播放器在播放声音时尝试获取唤醒锁。
NotificationManagerService 源
NotificationPlayer 源
It seems that notification player tries to acquire wake lock when playing sound.
NotificationManagerService source
NotificationPlayer source
看起来好像您正在使用一项服务。也许服务运行时间太长,手机在发送通知之前又回到睡眠状态。我假设您正在使用 AlarmManager 使手机退出睡眠状态以检查更新?如果是这种情况,您只需声明
WAKE_LOCK
权限,如下所示:我以前遇到过这个问题,尤其是从互联网上的服务进行更新时。我找到的唯一解决方案是声明
WAKE_LOCK
权限。It looks as though you are using a service. Perhaps the service runs too long and the phone goes back to sleep before the notification could be sent. I assume you're using the AlarmManager to bring the phone out of sleep to check for updates? If that is the case, you are just going to have to declare the
WAKE_LOCK
permission like so:I have run into this issue before, especially when updating from services on the internet. The only solution I have found is to declare the
WAKE_LOCK
permission.看来只是部分固件的问题,目前只有魅族有问题。
It seems that it is an issue of some firmwares only, currently the problem has only Meizu.