我自己无法重现这个问题,但到目前为止已经有5个用户报告了它。 我最近发布了一个应用程序更新,将目标SDK从27更改为28,这肯定是其中的一部分原因。 所有5个用户都在某种Pixel设备上运行某种Android 9。
应用程序通过调用设置通知并调用NotificationManager.notify()来响应警报情况。 此通知引用了一个尝试在外部存储上播放音频文件的通知通道。 我的应用程序在清单中包含READ_EXTERNAL_STORAGE权限。 但由于它本身没有访问任何外部存储中的内容,因此它没有要求用户授予该权限。
当我在我的Pixel上进行此操作时,它运行得很好。 但是5个用户报告它抛出异常,例如
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.anei.cadpage/net.anei.cadpage.CadPageActivity}: java.lang.SecurityException: UID 10132 does not have permission to content://media/external/audio/media/145 [user 0]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: UID 10132 does not have permission to content://media/external/audio/media/145 [user 0]
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:1559)
at android.app.NotificationManager.notifyAsUser(NotificationManager.java:405)
at android.app.NotificationManager.notify(NotificationManager.java:370)
at android.app.NotificationManager.notify(NotificationManager.java:346)
at net.anei.cadpage.ManageNotification.show(ManageNotification.java:186)
at net.anei.cadpage.ReminderReceiver.scheduleNotification(ReminderReceiver.java:46)
at net.anei.cadpage.ManageNotification.show(ManageNotification.java:161)
at net.anei.cadpage.CadPageActivity.startup(CadPageActivity.java:211)
at net.anei.cadpage.CadPageActivity.onCreate(CadPageActivity.java:93)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
... 11 more
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.checkGrantUriPermissionLocked(ActivityManagerService.java:9752)
at com.android.server.am.ActivityManagerService.checkGrantUriPermission(ActivityManagerService.java:9769)
at com.android.server.notification.NotificationRecord.visitGrantableUri(NotificationRecord.java:1096)
at com.android.server.notification.NotificationRecord.calculateGrantableUris(NotificationRecord.java:1072)
at com.android.server.notification.NotificationRecord.<init>(NotificationRecord.java:201)
我已经告诉所有4个用户手动授权“存储”权限,据我所知,这解决了问题。但为什么需要这样做呢?我的应用程序本身没有访问外部存储器,也没有设置渠道配置要求它。如果需要READ_EXTERNAL_STORAGE权限,则通知管理器应该处理。
报告问题的用户正在运行以下版本: google/taimen/taimen:9/PQ1A.190105.004/5148680:user/release-keys google/crosshatch/crosshatch:9/PQ1A.190105.004/5148680:user/release-keys google/marlin/marlin:9/PQ1A.181205.002.A1/5129870:user/release-keys google/sailfish/sailfish:9/PQ1A.181205.002.A1/5129870:user/release-keys google/walleye/walleye:9/PQ1A.181205.002/5086253:user/release-keys
我运行的是 google/taimen/taimen:9/PQ1A.181205.002/5086253:user/release-keys, 似乎比其他人落后,更新到 google/taimen/taimen:9/PQ1A.190105.004/5148680:user/release-keys 不会改变任何东西。我的设备仍然可以正常工作。
以下是所有代码及其执行分支的一些提示。堆栈跟踪很清楚,异常是在notify()调用中抛出的,而中止是因为应用程序没有安全访问通道指定的音频文件。
// Build and launch the notification
Notification n = buildNotification(context, message);
NotificationManager myNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert myNM != null;
// Seems this is needed for the number value to take effect on the Notification
activeNotice = true;
myNM.cancel(NOTIFICATION_ALERT);
myNM.notify(NOTIFICATION_ALERT, n);
........
private static Notification buildNotification(Context context, SmsMmsMessage message) {
/*
* Ok, let's create our Notification object and set up all its parameters.
*/
NotificationCompat.Builder nbuild = new NotificationCompat.Builder(context, ALERT_CHANNEL_ID);
// Set auto-cancel flag
nbuild.setAutoCancel(true);
// Set display icon
nbuild.setSmallIcon(R.drawable.ic_stat_notify);
// From Oreo on, these are set at the notification channel level
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // False
// Maximum priority
nbuild.setPriority(NotificationCompat.PRIORITY_MAX);
// Message category
nbuild.setCategory(NotificationCompat.CATEGORY_CALL);
// Set public visibility
nbuild.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
// Set up LED pattern and color
if (ManagePreferences.flashLED()) {
/*
* Set up LED blinking pattern
*/
int col = getLEDColor(context);
int[] led_pattern = getLEDPattern(context);
nbuild.setLights(col, led_pattern[0], led_pattern[1]);
}
/*
* Set up vibrate pattern
*/
// If vibrate is ON, or if phone is set to vibrate
AudioManager AM = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
assert AM != null;
if ((ManagePreferences.vibrate() || AudioManager.RINGER_MODE_VIBRATE == AM.getRingerMode())) {
long[] vibrate_pattern = getVibratePattern(context);
if (vibrate_pattern != null) {
nbuild.setVibrate(vibrate_pattern);
} else {
nbuild.setDefaults(Notification.DEFAULT_VIBRATE);
}
}
}
if ( ManagePreferences.notifyEnabled()) { // false
// Are we doing are own alert sound?
if (ManagePreferences.notifyOverride()) {
// Save previous volume and set volume to max
overrideVolumeControl(context);
// Start Media Player
startMediaPlayer(context, 0);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
Uri alarmSoundURI = Uri.parse(ManagePreferences.notifySound());
nbuild.setSound(alarmSoundURI);
}
}
String call = message.getTitle();
nbuild.setContentTitle(context.getString(R.string.cadpage_alert));
nbuild.setContentText(call);
nbuild.setStyle(new NotificationCompat.InboxStyle().addLine(call).addLine(message.getAddress()));
nbuild.setWhen(message.getIncidentDate().getTime());
// The default intent when the notification is clicked (Inbox)
Intent smsIntent = CadPageActivity.getLaunchIntent(context, true);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, smsIntent, 0);
nbuild.setContentIntent(notifIntent);
// Set intent to execute if the "clear all" notifications button is pressed -
// basically stop any future reminders.
Intent deleteIntent = new Intent(new Intent(context, ReminderReceiver.class));
deleteIntent.setAction(Intent.ACTION_DELETE);
PendingIntent pendingDeleteIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
nbuild.setDeleteIntent(pendingDeleteIntent);
return nbuild.build();
}
最新消息。昨晚我发布了一个更新,将目标SDK从28回退到了27。在过夜期间,又有两个用户报告了这个问题:他们使用运行Android 9的Pixel手机,并且正在运行版本针对SDK 28的应用程序。其中一个用户回复我确认,在安装了应用程序SDK 27版本后,问题消失了。这证实了这是一个与应用程序针对SDK 28相关的问题,可能与禁止应用程序使用全局访问文件系统权限来打破应用程序沙盒限制的更改有关。
为什么它会影响某些用户而不影响其他用户仍然是一个谜。特别是对于我。当我有时间时,我会再次尝试在我的手机上重现这个问题。有两个理论: 1)只有从未授予READ_EXTERNAL_STORAGE权限的人才会受到影响。我的设备原本被授予该权限,但在尝试重现问题时我已经撤销了授权。 2)只有当应用程序设置通知渠道并使用外部音频文件时才会发生此问题。对于大多数用户来说,这是正确的,但在我的情况下,声音文件是手动设置的。
content://
URI调用了NotificationChannel#setSound
。稍后调用NotificationCompat.Builder#setSound
或手动更改通道中的声音(通过UI)不会导致问题。另请参见:https://dev59.com/Jbbna4cB1Zd3GeqPUAXQ - caw