Android phone screen tapping unlock function code _android

Source: Internet
Author: User
Tags abs id3 tag

1. Foreword

Now there are a lot of Android phones to support the screen unlock, tapping screen unlock is a very useful function, but a dozen support tapping screen, and then only for unlock or lock screen, and then our application layer developers can not go in, completely unable to play up. Developers, developers, since we're developers, why not make some big news, so this time I will teach you how to use code to achieve the phone's tap recognition, it sounds interesting, some eager to. In fact, there is already an application for this function on iOS: Knock, a knock to unlock the Mac computer app for 4.99 dollars, about 33 yuan. Sometimes really want to do iOS development, can be happy for their application pricing, pleasure to earn extra money. Anyway, since iOS can be implemented, we will not be outdated Android, and now I lead you to use the code to achieve the tap recognition of the phone.

This article takes Java as the sample language and Android as the sample platform.

2. Function realization

2.1. Realize the idea

When it comes to knock recognition, what do you think you're going to do with the sensor? Yes, well, as the only way to identify mobile gestures, we naturally need to use sensors to recognize the impact, but there are a wide range of Android sensors, which one should we choose?

In the Android2.3 era, the Android system has defined 11 sensors, in the current era of Android6.0, the number of sensors has reached 26, so many sensors we use which one, in fact, we only need to consider the 2.3 era of the 11 sensors available, because one side Late-stage sensor components such as heartbeat sensors need hardware support, resulting in many mobile phones can not support such sensors, on the other hand, the 2.3-era 11 sensor function has been quite strong, can support the majority of gesture gesture recognition, so now I would like to enumerate the above 11 sensors:

Sensor_type_accelerometer acceleration
Sensor_type_magnetic_field Magnetic
sensor_type_orientation Direction
Sensor_type_  Gyroscope Gyroscope
Sensor_type_light Light induction
sensor_type_pressure pressure
sensor_type_temperature temperature 
Sensor_type_proximity approach
sensor_type_gravity gravity
sensor_type_linear_acceleration linear acceleration
sensor_type _rotation_vector rotation vector

For a detailed description of these 11 sensors, you can go to http://www.jb51.net/article/87940.htm to view the fact that I have always suspected LG G3 's percussion unlock is related to the light sensor or proximity sensor as I suspend my fingers in LG G3 's head is not able to be unlocked at the top, it is removed and returned to normal, and the tapping of the lock screen should only be related to the touch screen, because no matter how I block the sensor, the function of striking the lock screen is completely unaffected.

To get to the point, after understanding the 11 sensors, we need to choose which or what sensors to implement the function, we simulate the situation of the mobile phone, the phone flat on the desktop, finger tapping on the phone, the finger gave the phone a force, while the desktop to give the phone a reaction, Considering that the desktop is not deformed, mobile phone force balance acceleration of 0, but then the mobile phone acceleration sensor data will be changed, the answer is yes, the mobile phone acceleration sensor data will have a brief but significant changes, why, the mobile phone force balance acceleration of 0 because it is a whole, But the internal components will be affected by the complex force between the left and right, not the force at the same time to achieve the force balance, in fact, change the train of thought. With a similar interior smooth container, the container contains a few glass balls, tapping a few, the container will not move, but the glass ball is not moved. Although the internal components of mobile phones are much more stable than glass balls, they also have to follow the Basic Law and accept the role of honesty.

The above scene is flat on the desktop scene, real-life scenarios tend to be more complex and diverse, but no matter what the scenario, there is no doubt that the tapping of the phone should lead to significant changes in the data from the accelerometer, so we now understand what sensors should be selected as our tool for tapping. But the acceleration-related sensors are two, acceleration sensors and linear acceleration sensors, which one should we choose, the accelerometer provides data that is the acceleration of the mobile phone under gravity, the data provided by the linear acceleration sensor is the acceleration of the mobile phone that excludes gravity, and can intuitively reflect the force of the mobile phone after the removal of gravity. , which is suitable for tapping and identifying, should we choose a linear accelerometer, instead, we have to select the accelerometer, the linear accelerometer provided by Android is software-based, and the different platforms are not necessarily the same for linear acceleration sensors, in fact, knocking at Samsung S4,LG G3 in the back of a model, there is no significant change in the data from the linear acceleration sensor, for the sake of insurance, we still use the hardware based acceleration sensor more appropriate. By the way, when I saw the pressure sensor, I thought the sensor, which was used to monitor the pressure on the phone, was a good fit to identify the tap, and then I saw the description before I knew it was monitoring the pressure.

As mentioned above, the tapping operation of the mobile phone will cause the acceleration sensor outgoing data changes significantly, so the realization of this function, to determine whether there is a percussion method is to detect the mobile phone linear acceleration compared to the normal situation whether there is a significant change. In order to eliminate the effect of gravity in the process of function, it is necessary to process the data of the accelerometer to convert it into linear acceleration, because the conversion to linear acceleration is a process requiring calibration, so we need to put a certain amount of data to calibrate to get more accurate linear acceleration. At the same time, considering the existence of real life can lead to false recognition of the scene, such as shaking the phone will bring to the phone a longer time and obvious linear acceleration changes, so the concept of the steady state, the mobile phone in a relatively stable, no long time the obvious linear acceleration changes in the situation as a steady In the case of steady state, the recognition of percussion is performed. In addition, this percussion recognition takes into account the possibility of tapping the mobile phone's border too low, so only consider the tapping on the screen or back of the phone, so that the data of the x,y axis can be ignored in the process of recognition, only the linear acceleration of the z axis is considered.

2.2. Function Introduction

The function of this implementation is to identify the phone screen or the back of the tapping operation, function realization process: Registers the sensor, gathers the data, puts in the specified number of data calibration to obtain the more accurate linear acceleration, after the calibration finishes determines whether the current steady state, if is the unsteady state, waits for the next data, if is steady state, Then the calling method determines whether there is a percussion operation, and the linear acceleration and the number of recent taps are processed at the same time, and the steady state is displayed to the interface.

2.3. Function realization

2.3.1. Get Sensor data

The method of registering the sensor belongs to the system native method, does not explain too much, but need to note that the parameter that identifies the sensor data sampling interval when registering the acceleration sensor is best to use sensor_delay_game, because the acceleration data that the percussion causes changes very short, if use Sensor_ DELAY_UI or sensor_delay_normal often do not capture the acceleration changes caused by percussion, of course, if the use of sensor_delay_fastest naturally will not have this problem, but the performance consumption will be relatively large.

After registering the sensor, you can wait for the data to be processed in the callback method, I give the implementation code and the Code to explain the implementation process.

public void onsensorchanged (Sensorevent sensorevent) {if (sensorevent.sensor = = null) {return;} if (Sensorevent.sensor . GetType () = = Accelerometersensortype) {float Accelerationz = sensorevent.values[2]; if (Accelerationz > 0) {recognit
Ionknockratio = 20;
Recognitionuniqueratio = 10;
Smoothsectionmaxratio = 5f; else {recognitionknockratio = 7.5f; recognitionuniqueratio = 6; smoothsectionmaxratio = 2.5f;} gravityz = Alpha * Grav
Ityz + (1-alpha) * ACCELERATIONZ;
Linearaccelerationz = Accelerationz-gravityz; if (calibratelinearacceleration) {calibratelinearaccelerationindex++; if (Calibratelinearaccelerationindex <=
Calibratelinearaccelerationsectionnumber) {return;} calibratelinearacceleration = false; } if (Sensordatashowindex >= sensordatashownumber) {sensordatashowindex = Sensordatashownumber-
Sensordatashowdurationnumber;
Iterator<?> it = linearaccelerationzshowlist.listiterator (0); for (int i = 0; i < Sensordatashowdurationnumber i++) {it.next ();
It.remove ();
} mainactivity.updatesensordata (Linearaccelerationzshowlist);
} linearaccelerationzshowlist.add (Linearaccelerationz);
sensordatashowindex++; if (!stable) {linearaccelerationzlist.add (Linearaccelerationz); if (Linearaccelerationzlist.size () >=
Stablesectionnumber) {stablerecognition (); Linearaccelerationzlist.clear ();} return;
} knockrecognition (Linearaccelerationz); }
}

In the method of sensor data callback, the data obtained by the accelerometer are processed separately, firstly, according to the positive and negative of the z-axis acceleration, the Recognitionknockratio,recognitionuniqueratio Smoothsectionmaxratio Three variables give different values, as to why this is done because the actual tapping operation on the Android phone shows that the accelerometer is sensitive to the feedback of the positive tapping operation, and that the feedback on the back percussion is relatively slow, The feedback to the data level is that the acceleration sensor data changes caused by the percussion are significantly more than the back of the tap, so there are different values to be assigned to the tapping screen and the back of the tap, but in fact the cell phone's angle, Using the current data is completely unable to analyze the percussion caused by the acceleration of the obvious changes from the percussion or percussion back, so use the positive and negative of the z-axis acceleration to simply judge, after all, most of the z-axis acceleration is positive, which is the back of the phone to the ground, the user is more The negative is that the phone screen biased to the ground, the user is more likely to tap the back of the phone. The reason for this is that there are two different reasons for the feedback sensitivity of the tapping screen and the back acceleration sensor, one is that the accelerometer is closer to the screen than the back, and it's the Android phone shell, which is particularly evident in LG G3, where LG G3 has a curved plastic shell. , the sensor data that is triggered on the back is much lower than the tapping screen, while the metal-shell Samsung S6, which is triggered by the back-tapping of the sensor data, is close to tapping the screen. In fact, the above three coefficients belong to the empirical coefficient, and for different types of mobile phones to provide a different number of values, the reason can be seen just the LG G3 and Samsung S6, once again feeling the diversity of Android phones, Android phones too many kinds, The difference in hardware design makes it possible to apply a factor on a mobile phone that may not work on another phone, and if it's the same as the iphone, there's no doubt it's a lot better.

Then the acceleration is filtered to obtain linear acceleration, and the method of obtaining linear acceleration is referred to the proposed method in the Android Sensorevent source code:

* <p> * It should be apparent the "in" to measure "the real acceleration of *" Device, the contribution of T
He force of gravity must is eliminated. * This can is achieved by applying a <i>high-pass</i> filter.
Conversely, A * <i>low-pass</i> filter can be used to isolate the force of gravity. * </p> * * <pre class= "Prettyprint" > * * public void onsensorchanged (Sensorevent event) * {*//Alpha is Calc Ulated as T/(t + DT) *//with T, the Low-pass filter ' s time-constant *//And DT, the event delivery rate * Final Flo
at alpha = 0.8;
* * Gravity[0] = Alpha * Gravity[0] + (1-alpha) * event.values[0];
* GRAVITY[1] = Alpha * Gravity[1] + (1-alpha) * event.values[1];
* GRAVITY[2] = Alpha * gravity[2] + (1-alpha) * event.values[2];
* * Linear_acceleration[0] = event.values[0]-gravity[0];
* Linear_acceleration[1] = event.values[1]-gravity[1];
* Linear_acceleration[2] = event.values[2]-gravity[2]; * * </pre>

Through high pass filtering and low-pass filtering, the acceleration is processed to eliminate the gravitational effect to obtain the linear acceleration, but in the process it is necessary to pass a certain amount of data to calibrate to obtain more accurate linear acceleration. Here we set the Calibratelinearaccelerationsectionnumber as the data length to calibrate the data, Use Calibratelinearaccelerationindex and calibratelinearacceleration to control when the calibration ends.

After calibration, use linearaccelerationzshowlist storage to display the linear acceleration of the sensor on the application interface, then, if in unsteady state, start steady state recognition, determine whether the current state is stable, if in steady state, start tapping recognition.

2.3.2. Steady state identification

As mentioned above, if a user does something like shake a cell phone, is to produce obvious acceleration changes, it is likely to lead to false recognition of the situation, so the concept of steady state, that is, the acceleration of the phone does not have a long period of significant changes in the state, extended to the real scene is the user did not move the mobile phone significantly state, Strictly speaking, the average user in the mobile phone is significantly mobile, such as shaking the phone at the same time the possibility of tapping operation is very low, so the concept of homeostasis can be formally applied to the implementation of the function.

Already understand the concept of homeostasis, then how should we define what the situation is steady state, what the situation is unsteady, I give the implementation code, integrated code to explain.

private void Stablerecognition () {int exceptionnumber = 0; float accelerationzvalue; Float minaccelerationzvalue = Integ Er.
Max_value;
float maxaccelerationzvalue = Integer.min_value; for (int i = stableSectionNumber-1 i >= 0; i--) {accelerationzvalue = Linearaccelerationzlist.get (i); if (Math.Abs ( Accelerationzvalue) > Maxstableoffset) {exceptionnumber++;} else {if (Accelerationzvalue > Maxaccelerationzvalue {maxaccelerationzvalue = Accelerationzvalue} else {if (Accelerationzvalue < Minaccelerationzvalue) {MinAccelerat
Ionzvalue = Accelerationzvalue;
}}} stable = Exceptionnumber <= maxexceptionnumber; if (Stable) {if (linearaccelerationzstablesection = = 0) {linearaccelerationzstablesection = (maxaccelerationzvalue-min
Accelerationzvalue)/2;
} if (Linearaccelerationzstablesection > Maxstableoffset) {linearaccelerationzstablesection = MaxStableOffset;}}
Mainactivity.updatestable (Stable);
LogFunction.log ("Stable", "" "+ Stable); LogFunction.log ("EXceptionnumber "," "+ Exceptionnumber); LogFunction.log ("Linearaccelerationzstablesection", "" "+ Linearaccelerationzstablesection);}

In the realization of this function, the way to judge the steady state is to sample 50 points, then calculate the absolute value of each point, if greater than the maximum deviation maxstableoffset is regarded as an anomaly, the anomaly is greater than the maximum number of outliers maxexceptionnumber is considered unsteady, The converse is considered steady. When the steady state is over, half of the difference between the maximal z-axis and the minimum acceleration is regarded as the wave interval linearaccelerationzstablesection if the state is in stable. Maxstableoffset and Maxexceptionnumber are the same empirical coefficients, which are derived from the linear acceleration analysis of the different scenarios that the Android phone actually provides. Now there is a problem, that is, if the original state is in stable condition, and then the user suddenly to the mobile phone operation, the mobile phone state to the unsteady that how to deal with, do not worry, this problem will be in the process of tapping the recognition processing.

2.3.3. Tapping recognition

Now to the core of the entire function: tapping recognition, as mentioned above, can cause a noticeable change in accelerometer data, but how do we use the code to detect the taps and how to troubleshoot the user's false identification problems caused by other actions of the phone, which are actually dealt with here. Now I give the implementation code, integrated code to explain.

 private void knockrecognition (float Linearaccelerationz) {float
Linearaccelerationzabsolute = Math.Abs (Linearaccelerationz);
float Linearaccelerationzabsoluteradio = linearaccelerationzabsolute/linearaccelerationzstablesection; if (Linearaccelerationzabsoluteradio > Recognitionuniqueratio) {uniquelinearaccelerationzlist.add (
LINEARACCELERATIONZ);
Currentforecastnumber = Forecastnumber;  } else {if (uniquelinearaccelerationzlist.size () > 0) {if (Currentforecastnumber > 0) {currentforecastnumber--;}
else {Handleuniquelinearaccelerationz ();}}
} if (Linearaccelerationzabsoluteradio < smoothsectionmaxratio) {float offsetweight = 0.001f;
Linearaccelerationzstablesection = Weightedmean (Offsetweight, Linearaccelerationzabsolute,
Linearaccelerationzstablesection); }
}

Knockrecognition is the method used to process linear acceleration to confirm whether there is a percussion operation, first, the linear acceleration of the incoming parameter is processed to obtain the absolute value of linear acceleration, Then if the ratio of the absolute value of the linear acceleration to the fluctuation range is greater than the recognitionuniqueratio, then the cell phone is being subjected to force and the linear acceleration is added to the unique linear acceleration list for the purpose of determining whether the action is a tap or a user else. Conversely, if less than or equal to Recognitionuniqueratio, it is believed that the mobile phone in a relatively stable state, at this time if the unique linear acceleration list length greater than 0, if the Currentforecastnumber is greater than 0, The Currentforecastnumber minus 1, if the currentforecastnumber is less than or equal to 0, begins to process the unique linear acceleration list, while the process of handling the unique linear acceleration list formally begins to recognize whether the percussion, and whether the current state is converted to Non-steady state. At the same time, if the ratio of the absolute value of the linear acceleration to the fluctuation interval is less than smoothsectionmaxratio, the linear acceleration absolute value is used to smooth the fluctuation interval.

Here, you certainly have questions about currentforecastnumber, what the meaning of this variable is, why there is this variable, the reason for this is that a knock can result in two of unique linear accelerations that are close but discontinuous. Without currentforecastnumber, this variable can cause a real-world strike to be recognized as a two-tap operation.

And if the ratio of the absolute value of the linear acceleration to the fluctuation interval is less than smoothoffsetmaxratio, the linear acceleration absolute value is used to smooth the fluctuation interval, because the mobile phone's state may change at any time, and the fluctuation range should change with the mobile phone's state. On the other hand steady-state identification of the calculation of the wave range may be problematic, and can not correctly reflect the current mobile phone acceleration fluctuations, this time need to be based on the latest data to learn to smooth the range of fluctuations, And why the ratio is less than smoothsectionmaxratio because the ratio is greater than the smoothsectionmaxratio of the basic is abnormal linear acceleration, not suitable for smooth range of fluctuations, And if the real situation of linear acceleration and wave interval ratio is basically more than smoothsectionmaxratio, which means that the phone is now mostly in an unsteady state, waiting for a new steady-state recognition to reset the range of fluctuations, as stated above, Recognitionuniqueratio,smoothoffsetmaxratio is an empirical coefficient and can be set independently.

private void Handleuniquelinearaccelerationz () {LogFunction.log ("linearaccelerationzstablesection", "" +
Linearaccelerationzstablesection);
int recognitionknocknumber = 1;
int uniquelinearaccelerationzlistlength = Uniquelinearaccelerationzlist.size ();
float Accelerationzoffsetabsolute;
float Maxaccelerationzoffsetabsolute = 0; for (int i = 0; i < uniquelinearaccelerationzlistlength i++) {Accelerationzoffsetabsolute = Math.Abs (uniquelinearacce
Lerationzlist.get (i)); if (Maxaccelerationzoffsetabsolute < Accelerationzoffsetabsolute) {Maxaccelerationzoffsetabsolute =
Accelerationzoffsetabsolute;
} LogFunction.log ("Uniquelinearaccelerationzlist index" + I, "" + uniquelinearaccelerationzlist.get (i));}
Uniquelinearaccelerationzlist.clear ();
LogFunction.log ("Uniquelinearaccelerationzlistlength", "" "+ uniquelinearaccelerationzlistlength);
if (Uniquelinearaccelerationzlistlength > unstablelistlength) {stable = false;
Mainactivity.updatestable (Stable);
Return } LogFunction.log ("MaXaccelerationzoffsetabsolute/linearaccelerationzstablesection "," "+ (Maxaccelerationzoffsetabsolute/
linearaccelerationzstablesection)); if (Maxaccelerationzoffsetabsolute > Linearaccelerationzstablesection * recognitionknockratio) {LogFunction.log ("
Recognitionknockratio "," "+ recognitionknockratio);
LogFunction.log ("Recognitionuniqueratio", "" "+ recognitionuniqueratio);
Knockrecognitionsuccess (Recognitionknocknumber); }
}

Finally to the final Handleuniquelinearaccelerationz method, as the name implies, is used to deal with the unique linear acceleration list, in this method, the strike recognition and steady state whether the change of the decision, If the unique linear acceleration list length exceeds the unsteady-state unique linear acceleration list length, it is assumed that the current state of the mobile phone state transitions to an unsteady and ending method, if the maximum offset in the acceleration offset data list exceeds a certain multiple of the range of the fluctuation, it is recognized as a knock.

3. Summary

So far, the process of tapping the recognition is over. As a matter of fact, I provide the method of tapping recognition is still a case of false recognition, iOS knock I used, have the ability to match the price, the recognition rate is quite good, do not know whether they are machine learning or other methods to boil down a set of their identification coefficient, Of course I am here to provide the percussion recognition is only a method of tapping, I can not say that it is mature, because there is no real user test, we can completely according to their own ideas to replace the algorithm or even replace the sensor to achieve their own percussion recognition, and I am in fact equivalent to provide a realization.

This is the third blog, the first blog belongs to the test water has chosen to do a relatively partial door but not good to deal with a small module: write the ID3 tag for MP3 file, the second blog chooses a very rigorous practical module: audio synthesis, the first two modules have one thing in common is that the various specifications have been very clear, Although the implementation of the code may be different, but the implementation of the same thinking necessarily, and the third blog of the tap detection is undoubtedly loose a lot, so I was also the first time to write the realization of the idea of this section, because I am not sure that my implementation of the idea is completely correct, as a practical application of the sensor is the existence of countless possibilities, We can totally follow our own ideas to try, the wrong big deal in a different direction.

The above is a small set to introduce the Android mobile phone screen tapping unlock the implementation code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.