java.lang.SecurityException:需要Jelly Bean 4.2的VIBRATE权限

时间:2022-10-12 00:07:13

Since yesterday I have an issue on Android 4.2 when I receive push notifications it requires the permission even if i don't set it to vibrate

从昨天起我在Android 4.2上遇到问题,当我收到推送通知时,即使我没有将其设置为振动,也需要获得许可

Notification notification = new Notification(icon, notificationItem.message, when);
notification.setLatestEventInfo(context, "App", notificationItem.message,
            PendingIntent.getActivity(context, 0, intent, 0));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;

NotificationManager nm =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(notificationItem.notificationID, notification);

the exception is raised by nm.notify

nm.notify引发了异常

I have this issue in two different apps and i never modify the code

我在两个不同的应用程序中遇到此问题,我从不修改代码

3 个解决方案

#1


21  

This was a bug in Android 4.2 due to a change in the notification vibration policy; the permission bug was fixed by this change in 4.2.1.

由于通知振动策略发生变化,这是Android 4.2中的一个错误;权限错误由4.2.1中的此更改修复。

#2


37  

I got the same Exception in Jelly Bean 4.1.2, then following changes I made to resolve this

我在Jelly Bean 4.1.2中得到了相同的异常,然后是我为解决这个问题所做的更改

1.added permission in manifest file.

1.清单文件中的添加权限。

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

2.Notification Composing covered by Try-Catch

2. Try-Catch涵盖的通知组成

 try
    {
        mNotificationManager = (NotificationManager)          
        this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        this)
                .setSmallIcon(R.drawable.ic_notif_alert)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setStyle(bigTextStyle)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(true);
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            Log.d(TAG, "---- Notification Composed ----");
    }
    catch(SecurityException se)
    {
        se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

#3


24  

Since this bug only occurs on Android 4.2 and 4.3 you might use this as a workaround (i.e. include the maxSdkVersion):

由于此错误仅发生在Android 4.2和4.3上,您可以将其用作解决方法(即包含maxSdkVersion):

<uses-permission android:name="android.permission.VIBRATE" android:maxSdkVersion="18"/>

Note: the maxSdkVersion attribute was only added in API level 19, which in this case is luckily exactly the minimum we want! In theory we could put any value <= 18 to get the same effect, but that would be nasty.

注意:maxSdkVersion属性仅在API级别19中添加,在这种情况下幸运的是我们想要的最小值!从理论上讲,我们可以将任何值<= 18来获得相同的效果,但那将是令人讨厌的。

#1


21  

This was a bug in Android 4.2 due to a change in the notification vibration policy; the permission bug was fixed by this change in 4.2.1.

由于通知振动策略发生变化,这是Android 4.2中的一个错误;权限错误由4.2.1中的此更改修复。

#2


37  

I got the same Exception in Jelly Bean 4.1.2, then following changes I made to resolve this

我在Jelly Bean 4.1.2中得到了相同的异常,然后是我为解决这个问题所做的更改

1.added permission in manifest file.

1.清单文件中的添加权限。

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

2.Notification Composing covered by Try-Catch

2. Try-Catch涵盖的通知组成

 try
    {
        mNotificationManager = (NotificationManager)          
        this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        this)
                .setSmallIcon(R.drawable.ic_notif_alert)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setStyle(bigTextStyle)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(true);
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            Log.d(TAG, "---- Notification Composed ----");
    }
    catch(SecurityException se)
    {
        se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

#3


24  

Since this bug only occurs on Android 4.2 and 4.3 you might use this as a workaround (i.e. include the maxSdkVersion):

由于此错误仅发生在Android 4.2和4.3上,您可以将其用作解决方法(即包含maxSdkVersion):

<uses-permission android:name="android.permission.VIBRATE" android:maxSdkVersion="18"/>

Note: the maxSdkVersion attribute was only added in API level 19, which in this case is luckily exactly the minimum we want! In theory we could put any value <= 18 to get the same effect, but that would be nasty.

注意:maxSdkVersion属性仅在API级别19中添加,在这种情况下幸运的是我们想要的最小值!从理论上讲,我们可以将任何值<= 18来获得相同的效果,但那将是令人讨厌的。