我希望在点击按钮时使我的设备振动。但是它总是抛出一个
我知道从API 23开始,应用程序必须在运行时请求一些权限,但是根据这个链接,Vibrate权限是无需要求即可授予的。
有什么想法吗?
方法代码:
java.lang.SecurityException: Requires VIBRATE permission
错误,尽管我已经在清单文件中获取了VIBRATE权限。我知道从API 23开始,应用程序必须在运行时请求一些权限,但是根据这个链接,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)
onOptionsItemSelected()
中的vibrate()
的废弃项目,并且在运行 Android 7.1.1 的 Nexus 9 上,它可以正常工作,需要VIBRATE
权限。 - CommonsWarebuild/intermediates/manifests/full/...
下检查合并的清单。 - laalto