java.lang.SecurityException: Requires VIBRATE permission on Jelly Bean 4.2
Solution 1
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.
Solution 2
I got the same Exception in Jelly Bean 4.1.2, then following changes I made to resolve this
1.added permission in manifest file.
<uses-permission
android:name="android.permission.VIBRATE"></uses-permission>
2.Notification Composing covered by 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();
}
Solution 3
Since this bug only occurs on Android 4.2 and 4.3 you might use this as a workaround (i.e. include the 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.
Ste
Updated on March 14, 2020Comments
-
Ste over 4 years
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
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
I have this issue in two different apps and i never modify the code
-
AlikElzin-kilaka over 10 yearsJust got a crash report from the market, where it happened a SGS4 (4.3). I don't set vibration either.
-
Yoann Hercouet about 10 yearsSame thing here, it does not seem to be fixed on 4.3 either.
-
Diederik over 9 yearsIsn't either 1 or 2 sufficient to fix this problem. I'd prefer only 1, if your users will be OK with a bit of permission creep.
-
swiftBoy over 9 years@Diederik see the answer I have shared is best practices for handling this issue in my eyes, its up-to you how you want do it, No matter 1 or 2, app should work on all the device is the need
-
zulkarnain shah over 8 yearsI think solution 1 (adding permission in AndroidManifest.xml file) should be enough
-
snachmsm almost 7 yearscan you confirm that above declaration with
android:maxSdkVersion="18"
attr will let install app on devices without Vibrator? because line with only<uses-permission android:name="android.permission.VIBRATE" />
causes app (update) not available -
Mark almost 7 yearssorry, I don't know.
-
Sorin Bolos almost 6 years@snachmsm did you ever check to see if this fix works on devices without vibration?
-
Mark almost 6 yearsI don't know, but in terms of allowing installation, I believe only uses-feature (not uses-permission) affects that.
-
snachmsm almost 6 years@Sorin I'm confirming Marks comment, fix works as it uses
uses-permission
, notuses-feature