Android Application Realizes Click Button Vibration

  • 2021-12-19 06:53:25
  • OfStack

This project shares the specific code of Android application to realize click button vibration for your reference, the specific contents are as follows

In the project, it is necessary to click the buttons in the application, which has a vibrating effect. Vibration effect can be turned off in settings.

The specific implementation is as follows.

Write a vibration helper class: VibrateHelp. java


import android.content.Context;
import android.os.Vibrator;
 
/**
 *  Vibration helper class 
 * androidManifest.xml Add to   The following permissions 
 * <uses-permission android:name="android.permission.VIBRATE" />
 */
public class VibrateHelp {
 private static Vibrator vibrator;
 
 /**
  *  Simple vibration 
  * @param context      Call the vibrating Context
  * @param millisecond  Time of vibration, milliseconds 
  */
 @SuppressWarnings("static-access")
 public static void vSimple(Context context, int millisecond) {
  vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
  vibrator.vibrate(millisecond);
 }
 
 /**
  *  Complex vibration 
  * @param context  Call the vibrating Context
  * @param pattern  Vibration form 
  * @param repeate  The number of vibrations, -1 Non-repetitive, non-repetitive -1 For from pattern Begins to duplicate the specified subscript of 
  */
 @SuppressWarnings("static-access")
 public static void vComplicated(Context context, long[] pattern, int repeate) {
  vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
  vibrator.vibrate(pattern, repeate);
 }
 
 /**
  *  Stop vibrating 
  */
 public static void stop() {
  if (vibrator != null) {
   vibrator.cancel();
  }
 }
}

The following custom implementation button clicks the vibration class: ViewClickVibrate. java


import android.view.View;

import android.view.View.OnClickListener;
 
public class ViewClickVibrate implements OnClickListener{
 /**  Button vibration time  */
 private final int VIBRATE_TIME = 60;
 
 
 @Override
 public void onClick(View v) {
  // TODO  Determining whether to perform vibration according to the flag in the setting 
  VibrateHelp.vSimple(v.getContext(), VIBRATE_TIME);
 }
}

Finally, when setting listening for buttons, use the class ViewClickVibrate, as follows: MainActivity. java


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
 Button btn1;
 Button btn2;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  btn1 = (Button) findViewById(R.id.button1);
  btn2 = (Button) findViewById(R.id.button2);
  btn1.setOnClickListener(new ViewClickVibrate() {
   public void onClick(View v) {
    super.onClick(v);
    // TODO
   }
  });
  btn2.setOnClickListener(new ViewClickVibrate() {
   public void onClick(View v) {
    super.onClick(v);
    // TODO
   }
  });
 }
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}

You can also customize the click event of other controls to realize the click vibration effect, which is similar to the above change.


Related articles: