1 添加震动权限,在AndroidManifest.xml文件中添加:  <uses-permission android:name="android.permission.VIBRATE" />

Android 实现点击震动效果_android


2 代码

package com.test.ht;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

}

Vibrator vibrator;

@Override
public void onClick(View v) {
//震动30毫秒
vibrator.vibrate(30);
}

}