Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requires vibrate permission although permission is in manifest

I want to make my device vibrate on tapping on a button. But it always throws me the java.lang.SecurityException: Requires VIBRATE permission error although I have the VIBRATE permission in the manifest.

I know that from API 23 the apps have to ask on runtime for some permissions but according to this link the Vibrate permission is granted without having to ask.

Any Ideas?

Method code:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            // User chose the "Settings" item, show the app settings UI...
            return true;

        case R.id.share:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        case R.id.sync:
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500); //MainActivity line 832
            getRemoteItems();

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);

    }
}

AndroidManifest.xml code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pigmalionstudios.todolist">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
    <activity
        android:name=".Activities.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

EDIT: Error message:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.pigmalionstudios.todolist, PID: 3380
              java.lang.SecurityException: Requires VIBRATE permission
                  at android.os.Parcel.readException(Parcel.java:2004)
                  at android.os.Parcel.readException(Parcel.java:1950)
                  at android.os.IVibratorService$Stub$Proxy.vibrate(IVibratorService.java:163)
                  at android.os.SystemVibrator.vibrate(SystemVibrator.java:77)
                  at android.os.Vibrator.vibrate(Vibrator.java:178)
                  at android.os.Vibrator.vibrate(Vibrator.java:97)
                  at android.os.Vibrator.vibrate(Vibrator.java:76)
                  at com.pigmalionstudios.todolist.Activities.MainActivity.onOptionsItemSelected(MainActivity.java:832) //This line of code is v.vibrate(500);
                  at android.app.Activity.onMenuItemSelected(Activity.java:3450)
                  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:368)
                  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
                  at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:65)
                  at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:202)
                  at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
                  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
                  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
                  at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
                  at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
                  at android.view.View.performClick(View.java:6294)
                  at android.view.View$PerformClick.run(View.java:24770)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
like image 501
Benjamin Heinke Avatar asked Feb 07 '18 14:02

Benjamin Heinke


People also ask

Which permission is needed for use vibration service in android?

VIBRATE permission by adding <uses-permission android:name="android. permission. VIBRATE"/> to AndroidManifest. xml .

How do I declare permissions in android manifest?

Add declaration to app manifest To declare a permission that your app might request, include the appropriate <uses-permission> element in your app's manifest file. For example, an app that needs to access the camera has this line in AndroidManifest.

What is permissions in manifest?

The Android manifest file helps to declare the permissions that an app must have to access data from other apps. The Android manifest file also specifies the app's package name that helps the Android SDK while building the app.


1 Answers

I just met the same issue. The way to fix it is rebuild your app.

Changing code will cause react native refresh your app, but seems it won't reload your AndroidManifest.xml.

Thus, just manually run react-native run-android again.

like image 135
harrrrrrry Avatar answered Oct 07 '22 00:10

harrrrrrry