Android can trigger vibration HapticFeedback (vibration feedback) without permission

This article is authorized to publish by Harry Weasley.

HarryWeasley's blog address:

http://blog.csdn.net/harryweasley

When you long press a control in Android, you want to use vibration to remind the user. In addition to using the Vibrate class, you can also use (HapticFeedback) vibration feedback.

In this blog, let’s get familiar with Android vibration feedback. First, let’s turn on the vibration mode on the phone. Here I am using a Xiaomi phone to do the simulation. The location is in Settings—>Sound and Vibration—>Vibrate when touched, as follows The picture shows:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
I chose a stronger vibration intensity to make the vibration more obvious.

1 System triggers vibration

Let’s start this blog with an example and register a long press to listen to a button:

Android can trigger vibration HapticFeedback (vibration feedback) without permission

When you long press this button, a toast pops up and vibrates. However, returning false will not trigger the vibration.
Now look at the source code analysis, why is this.

button implements the setOnLongClickListener method, in the parent class View of the parent class TextView,

View.setOnLongClickListener source code:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
We need to see where mOnLongClickListener calls the onLongClick method of the interface, and finally find it in the View source code

View.performLongClick source code:

Android can trigger vibration HapticFeedback (vibration feedback) without permission

You can see that the onLongClick method is executed on line 13 and the return value is given to the variable handled.
On line 18, hangdled is true, and performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) is executed; this method finally triggers vibration feedback.

This is why, when onLongClick returns true, there will be a vibration effect.

2 Custom trigger vibration

As mentioned in the previous section, the vibration is triggered in performHapticFeedback. Observing the source code, we know that users can trigger it through code.

As shown below, clicking will also trigger vibration feedback:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
Now we go to the performHapticFeedback source code to see what has been executed,

View.performHapticFeedback source code:

Android can trigger vibration HapticFeedback (vibration feedback) without permission

Here are three knowledge points:

1. The vibration will only be triggered when isHapticFeedbackEnabled() is true. Later, I will explain why the vibration is not triggered when it is false.

In xml, it can be set by android:hapticFeedbackEnabled=”false|true”

In java code, it can be set by view.setHapticFeedbackEnabled(boolean),

But the default is true.

2. HapticFeedbackConstants constant value, we need to use three, one is LONG_PRESS (long press), the second is FLAG_IGNORE_VIEW_SETTING (not affected by the setting of view, that is, not affected by isHapticFeedbackEnabled()), the third is FLAG_IGNORE_GLOBAL_SETTING (not affected by system settings, that is, not affected by whether vibration feedback is turned on)

3. We see that the method finally returns the performHapticFeedback(int feedbackConstant, int flags) method,

View.performHapticFeedback(int feedbackConstant, int flags)源码:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
Look at the if statement on line 15, when flags=0, flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING is 0, and isHapticFeedbackEnabled() is false, the entire condition is true, so line 17 will be executed and return directly. This is why the performHapticFeedback(int feedbackConstant) method must trigger vibration only when isHapticFeedbackEnabled() is true.
Here to talk about it, & is a bitwise AND, returning a value, && logically, returning a Boolean value.
Lines 19-20 are the code that triggers the underlying vibration, and the code will not be analyzed after that.

3 HapticFeedbackConstants常量

Next, look at the three constants of HapticFeedbackConstants, or the previous code, as shown below:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
After clicking, a vibration will be triggered, but if the xml adds the sentence android:hapticFeedbackEnabled=”false”, there will be no vibration effect when clicked. As follows:

<Button
    android:layout_width="wrap_content"
    android:id="@+id/click"
    android:layout_height="wrap_content"
    android:hapticFeedbackEnabled="false"
    android:text="make" />

If you want to make it vibrate at this time, you can do it as follows:

Android can trigger vibration HapticFeedback (vibration feedback) without permission

Ignore the property settings of the view.

Remember before this article, I said to turn on the vibration on touch switch in the settings. In fact, if the user does not turn it on, it can still be vibrated. Just use the following method:

Android can trigger vibration HapticFeedback (vibration feedback) without permission

Ignore the system settings, haha, is it a perverted method, but it is not recommended, after all, the user forbids touch feedback, we don’t need to continue to challenge the user’s limit.

Finally, I would like to say that the above method does not require vibration authority, vibration authority, and vibration authority. Important things are said three times.

If you have a good article that you want to share with you, you are welcome to submit it, and you can directly submit the article link to me.

Welcome to press and hold the picture -> identify the QR code in the picture or scan and follow my official account:

Android can trigger vibration HapticFeedback (vibration feedback) without permission
Read the original

Guess you like

Origin blog.51cto.com/15064646/2575344