ANDROID_ (service) Vibrator Vibrator

Source: Internet
Author: User

The vibrator Vibrator is a service that Android gives us for the vibration of the fuselage , for example, when we receive a push message, we can set up a vibrating alert, or we can use it to enhance the interactivity of the player.

Run:

Program Structure

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.administrator.myapplication" > <Application Android:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:roundicon= "@mipmap/ic_launcher_round"Android:supportsrtl= "true"Android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&    Gt </application> <uses-permission android:name= "Android.permission.VIBRATE"/></manifest>
Androidmanifest.xml

 Packagecom.example.administrator.myapplication;ImportAndroid.app.Service;ImportAndroid.content.Context;ImportAndroid.os.Vibrator;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{PrivateButton Btn_hasvibrator; PrivateButton Btn_short; PrivateButton Btn_long; PrivateButton Btn_rhythm; PrivateButton btn_cancle; PrivateVibrator Myvibrator; PrivateContext Mcontext; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //obtain a vibrator instance of the system:Myvibrator =(Vibrator) Getsystemservice (Service.vibrator_service); Mcontext= Mainactivity. This;    Bindviews (); }    Private voidbindviews () {Btn_hasvibrator=(Button) Findviewbyid (r.id.btn_hasvibrator); Btn_short=(Button) Findviewbyid (R.id.btn_short); Btn_long=(Button) Findviewbyid (R.id.btn_long); Btn_rhythm=(Button) Findviewbyid (r.id.btn_rhythm); Btn_cancle=(Button) Findviewbyid (r.id.btn_cancle); Btn_hasvibrator.setonclicklistener ( This); Btn_short.setonclicklistener ( This); Btn_long.setonclicklistener ( This); Btn_rhythm.setonclicklistener ( This); Btn_cancle.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn_hasvibrator:toast.maketext (Mcontext, Myvibrator.hasvibrator ()? "Current device has vibrators": "Current equipment without vibrators", Toast.length_short). Show ();  Break;  CaseR.id.btn_short:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{100, 200, 100, 200}, 0); Toast.maketext (Mcontext,"Short Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_long:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{100, 100, 100, 1000}, 0); Toast.maketext (Mcontext,"Long Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_rhythm:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{500, 100, 500, 100, 500, 100}, 0); Toast.maketext (Mcontext,"Rhythm Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_cancle:myvibrator.cancel (); Toast.maketext (Mcontext,"Remove Vibration", Toast.length_short). Show (); }    }}
mainactivity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context= "Com.example.administrator.myapplication.MainActivity"Android:weightsum= "1" > <Button Android:id= "@+id/btn_hasvibrator"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Monitor whether the phone has vibrators"/> <Button Android:id= "@+id/btn_short"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Short Vibration"/> <Button Android:id= "@+id/btn_long"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Long Vibration"/> <Button Android:id= "@+id/btn_rhythm"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Rhythm Vibration"/> <Button Android:id= "@+id/btn_cancle"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Suppress Vibration"/></linearlayout>
Activity_main.xml

First, obtain the system vibrator

        Myvibrator = (vibrator) Getsystemservice (service.vibrator_service);         = Mainactivity. this;

Second, judge and set a different frequency of the vibrator

abstract void Cancel (): Close or stop the vibrator

Abstract Boolean Hasvibrator (): Determine if the hardware has a vibrator

void vibrate (long milliseconds): Controls phone vibration of milliseconds milliseconds

void vibrate (long[] pattern,int repeat): Specifies the mode vibration of the phone in pattern specified

First parameter: New int[200,400,600,800], alternately starting and shutting down the vibrator at 200,400,600,800 this time
Second parameter: Repeat number, if 1 is the only vibration once, if it is 0, it has been vibrating

  Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn_hasvibrator:toast.maketext (Mcontext, Myvibrator.hasvibrator ()? "Current device has vibrators": "Current equipment without vibrators", Toast.length_short). Show ();  Break;  CaseR.id.btn_short:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{100, 200, 100, 200}, 0); Toast.maketext (Mcontext,"Short Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_long:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{100, 100, 100, 1000}, 0); Toast.maketext (Mcontext,"Long Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_rhythm:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{500, 100, 500, 100, 500, 100}, 0); Toast.maketext (Mcontext,"Rhythm Vibration", Toast.length_short). Show ();  Break;  CaseR.id.btn_cancle:myvibrator.cancel (); Toast.maketext (Mcontext,"Remove Vibration", Toast.length_short). Show (); }    }

Third, open system permissions

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

ANDROID_ (service) Vibrator Vibrator

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.