我想点击一个按钮让我的设备振动。但它总是抛出java.lang.SecurityException: Requires VIBRATE permission
错误,尽管我在清单中有VIBRATE权限。
我知道,从API 23开始,应用程序必须在运行时询问某些权限,但根据this link,无需询问即可获得Vibrate权限。
任何想法?
方法代码:
@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代码:
<?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>
修改 错误讯息:
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)
答案 0 :(得分:0)
我刚刚遇到了同样的问题。修复它的方法是重建您的应用。
更改代码将导致本机响应刷新您的应用程序,但似乎不会重新加载您的AndroidManifest.xml。
因此,只需再次手动运行react-native run-android
。