NotificationManager.notify() 失败并出现 SecurityException

发布于 2024-11-25 02:49:53 字数 1144 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

电影里的梦 2024-12-02 02:49:53

似乎通知播放器在播放声音时尝试获取唤醒锁。

NotificationManagerService 源

NotificationPlayer 源

It seems that notification player tries to acquire wake lock when playing sound.

NotificationManagerService source

NotificationPlayer source

白衬杉格子梦 2024-12-02 02:49:53

看起来好像您正在使用一项服务。也许服务运行时间太长,手机在发送通知之前又回到睡眠状态。我假设您正在使用 AlarmManager 使手机退出睡眠状态以检查更新?如果是这种情况,您只需声明 WAKE_LOCK 权限,如下所示:

<uses-permission android:name="android.permission.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:

<uses-permission android:name="android.permission.WAKE_LOCK" />

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.

廻憶裏菂餘溫 2024-12-02 02:49:53

看来只是部分固件的问题,目前只有魅族有问题。

It seems that it is an issue of some firmwares only, currently the problem has only Meizu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文