我正在尝试创建一个Android应用程序,它会在按钮点击时启动振动。
下面是与振动相关的代码,我在Android清单文件中添加了uses-permission标记,但应用程序仍然终止给出SecurityException
public class MainActivity extends AppCompatActivity {
private Vibrator v;
TimerTask timerTask;
int n=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
public void startTimer(View view) {
final Handler handler = new Handler();
Timer mTimer = new Timer();
v.vibrate(2000);
timerTask = new TimerTask() {
@Override
public void run() {
boolean post = handler.post(new Runnable() {
@Override
public void run() {
TextView timer = (TextView) findViewById(R.id.timer);
timer.setText(n);
n++;
}
});
}
};
mTimer.schedule(timerTask, 0, 100);
}
logcat的:
06-17 03:41:12.962 8851-8851/com.neuroapp.ojas.neuroapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40c3ba68)
06-17 03:41:13.022 8851-8851/com.neuroapp.ojas.neuroapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3099)
at android.view.View.performClick(View.java:3571)
at android.view.View$PerformClick.run(View.java:14247)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3094)
at android.view.View.performClick(View.java:3571)
at android.view.View$PerformClick.run(View.java:14247)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Requires VIBRATE permission
at android.os.Parcel.readException(Parcel.java:1327)
at android.os.Parcel.readException(Parcel.java:1281)
at android.os.IVibratorService$Stub$Proxy.vibrateMagnitude(IVibratorService.java:290)
at android.os.Vibrator.vibrate(Vibrator.java:88)
at android.os.Vibrator.vibrate(Vibrator.java:57)
at com.neuroapp.ojas.neuroapp.MainActivity.startTimer(MainActivity.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3094)
at android.view.View.performClick(View.java:3571)
at android.view.View$PerformClick.run(View.java:14247)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)