System services provided by Android-Vibrator and android-vibrator

Source: Internet
Author: User

System services provided by Android-Vibrator and android-vibrator

Vibrator, a system service provided by Android)

-- Reprinted with the source: coder-pig



Vibrator introduction and related methods:






Simple demo -- set frequencyDifferent Vibrator


The most widely used app for Vibrator is the so-called mobile phone massager app, which is searched and piled up in the app market. I just took a few moments to look at it.

These are all similar.There are more than 8 million downloads... well, it seems that there are not too many downloads, but the general function is to switch the vibration frequency to complete.

It is unknown whether the so-called massage effect is effective.

Let's implement a simple massager!

The core is actually the parameter of the array in vibrate (). You can write an array as needed!

Because the simulator does not vibrate, it must be run on the mobile phone to achieve the effect!


:


The Code is also very simple. There are only four simple buttons for layout.

Activity_main.xml:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. vibratordemo. mainActivity "> <Button android: id =" @ + id/btnshort "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "Short vibration"/> <Button android: id = "@ + id/btnlong" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Long vibration"/> <Button android: id = "@ + id/btnrhythm" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "rhythm vibration"/> <Button android: id = "@ + id/btncancle" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "cancel vibration"/> </LinearLayout>

The next step is to write MainActivity, which is the same as the previous section. Let the Activity class implement the OnClickListener interface.

The onClick method can be rewritten to determine which button is clicked based on different view IDs. The advantage of this method is that there are many components.

This can simplify the code!

MainActivity. java:

Package com. jay. example. vibratordemo; import android. app. activity; import android. app. service; import android. OS. bundle; import android. OS. vibrator; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private Button btnshort; private Button btnlong; private Button extensions; private Button btncancel; private Vibrator myVibrator; @ jsonvoid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshort = (Button) findViewById (R. id. btnshort); btnlong = (Button) findViewById (R. id. btnlong); btnrhythm = (Button) findViewById (R. id. btnrhythm); btncancel = (Button) findViewById (R. id. btncancle); btnshort. setOnClickListener (this); btnlong. setOnClickListener (this); btnrhythm. setOnClickListener (this); btncancel. setOnClickListener (this); // obtain the Vibrator instance of the system: myVibrator = (Vibrator) getSystemService (Service. VIBRATOR_SERVICE) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btnshort: myVibrator. cancel (); myVibrator. vibrate (new long [] {100,200,100,200}, 0); Toast. makeText (getApplicationContext (), "short vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btnlong: myVibrator. cancel (); myVibrator. vibrate (new long [] {100,100,100,100 0}, 0); Toast. makeText (getApplicationContext (), "Long vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btnrhythm: myVibrator. cancel (); myVibrator. vibrate (new long [] {500,100,500,100,500,100}, 0); Toast. makeText (getApplicationContext (), "rhythm vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btncancle: myVibrator. cancel (); Toast. makeText (getApplicationContext (), "cancel vibration", Toast. LENGTH_SHORT ). show ();}}}

Do not forget to add permissions to the AndroidManifest. xml file!

<uses-permission android:name="android.permission.VIBRATE"/>



Well, the basic usage is actually very simple. I will not talk about it here. I also mentioned that the virtual machine has no vibration effect, so

You need to publish the application to your mobile phone to check the effect!Download the reference code: vibratorDemo.rar

For your convenience, you can export the apk directly. You can test the effect by directly downloading and installing the apk on your mobile phone. Of course, it's just a small demo.

Push ads, send text messages or something =-=! You can view required permissions during installation! Apk download:VibratorDemo.apk











Android software Vibration

If you have an idea, do your family know that you are so funny.

Since you insist on asking this question, although you have given so few scores, let's start talking about it slowly,

Of course, before talking about it, I still want to say that there is really a better idea (funny) year (better)
Well, first of all, let's start with Android software development. Here is an example.
In the Android system, the Vibrator object is responsible for handling mobile phone vibrations. The specific implementation method is as follows:
Obtain an instance of the Vibrator:

Vibrator vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );

GetSystemService (VIBRATOR_SERVICE): gets a vibrating service.

Then you get the desired part, adjust the vibration parameters, and now we get the vibrator,
We also need a parameter to generate a vibration
Only one parameter is passed to vibrate (), which is used to specify the number of milliseconds of Vibration

1. // vibrate for 5 seconds
2. vibrator. vibrate (5000 );
• Pass multiple parameters to the vibrate () method

2. // wait 1 second, shake 2 seconds, wait 1 second, shake 3 seconds
3. long [] pattern = {1000,200 0, 1000,300 0 };
4. //-1 indicates no repetition. If it is not-1, for example, if it is changed to 1, it indicates that the repeat starts from the element whose subscript of the long array is 1. 5. vibrator. vibrate (pattern,-1 );

Of course, you can't keep shaking him down, unless your phone is waterproof and you have some secret for your own personally use, you know.

We need to cancel the vibration.
Vibrator. cancel ();
It is better to have his permissions,
Add permissions to the AndroidManifest. xml file
<Uses-permission android: name = "android. permission. VIBRATE"/>
Do you understand. In fact, I don't understand either. I also want to tell you one more thing. It's good to play with anything. Don't play with code.

Some knowledge about Android porting and underlying driver changes

When various manufacturers develop Android-based products, although sometimes the Android framework needs to be modified, porting is the main task.
The Android system itself is a huge system. Porting does not need to be proficient in every part of Android. You need to consider the hardware abstraction layer (HAL) of Android and related device drivers in Linux. 1-3.
  

Figure 1-3 concept of Android porting

Android-based mobile phones include G1, G2, Hero, and Nexus One. Other mobile phone manufacturers have also released several Android phones. These mobile phones use different processors and peripheral hardware, however, most Android systems use the same software (including local frameworks, virtual machines, Java frameworks, and Java applications ).

The purpose of porting is to make minor changes to support a large upper-layer system. At the same time, the hardware abstraction layer has a standard interface, which can be referenced in the implementation of different platforms. Although the specific implementation content is different, the idea is similar and can be used for reference.
The Android system is transplanted to run the Android system on specific hardware. In the process of transplantation, it is important to grasp the key points and reduce the workload. From the perspective of work, the common method is to first familiarize yourself with the interfaces of the hardware abstraction layer, and then integrate and reuse existing drivers. The main workload is in the implementation of the hardware abstraction layer. In order to better understand and debug the system, you should also properly understand the upper-layer calls to the hardware abstraction layer.
There are two main steps for porting:
Linux driver
Android hardware abstraction layer
The drivers in Linux work in the kernel space, and the hardware abstraction layer of Android systems work in the user space. With the combination of these two parts, this allows a large Android system to run on a specific hardware platform.
The main task of porting Android is 1-4.
  

Figure 1-4 main tasks of Android porting

After a specific hardware system is available, it is usually necessary to implement its drivers in Linux. These drivers are usually standard drivers in Linux, the Android platform and other Linux platforms are basically the same. The main implementation is the Hardware abstraction Layer (Hardware Abstract Layer) in the Android system. The Hardware abstraction Layer calls the driver in Linux and provides interfaces to the driver, for other parts of the Android system (usually the Android local framework layer) to call.

Tip: the Android hardware abstraction layer interface is a local port layer interface. It is not a standard API and does not have forward or backward compatibility.
The content to be transplanted in the Android system mainly includes the following parts:
Display)
Including framebuffer driver + Gralloc module (optional)
User Input)
Including Event driver + EventHub (Android standard content)
Multimedia Codec)
Including hardware Codec driver + Codec plug-in (such as OpenMax)
3D Accelerator)
Including hardware OpenGL driver + OpenGL plug-in
Audio part (Audio)
Including the Audio driver + Audio Hardware Abstraction Layer
Video Out)
Including video display driver + Overlay Hardware Abstraction Layer
Camera (Camera)
Including the Camera driver (usually v4l2) + Camera Hardware Abstraction Layer
Phone)
Modem driver + RIL Library
GPS)
Including GPS Driver (usually serial port) + GPS Hardware Abstraction Layer
Wireless LAN (WIFI)
Including the Wlan driver and Protocol + WIFI adaptation layer (Android... the remaining full text>

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.