Android (Java) Learning Note 113:android Write code call vibrator Vibrate function, press button buttons always not vibrate

Source: Internet
Author: User

1. The code I wrote earlier is as follows:

1  Packagecom.himi.vibrate;2 3 Importandroid.app.Activity;4 ImportAndroid.app.Service;5 ImportAndroid.content.res.AssetManager;6 ImportAndroid.graphics.Typeface;7 ImportAndroid.os.Bundle;8 ImportAndroid.os.Vibrator;9 ImportAndroid.view.View;Ten ImportAndroid.view.View.OnClickListener; One ImportAndroid.widget.Button; A  -  Public classMainactivityextendsActivityImplementsOnclicklistener { -     PrivateButton Btnlow; the     PrivateButton Btnnormal; -     PrivateButton btnfast; -      - Vibrator Vibrator; +  - @Override +      Public voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -          -Vibrator =(Vibrator) Getsystemservice (service.vibrator_service); -          -Btnlow =(Button) Findviewbyid (r.id.btnlow); -Btnnormal =(Button) Findviewbyid (r.id.btnnormal); inBtnfast =(Button) Findviewbyid (r.id.btnfast); - //The following five lines of code is to implement the button above the text display in italics, regardless to          +Assetmanager mgr =getassets (); -Typeface tf = Typeface.createfromasset (Mgr, "Fonts/droidsansfallback.ttf"); the          * Btnlow.settypeface (TF); $ Btnnormal.settypeface (TF);Panax Notoginseng Btnfast.settypeface (TF); -  the } +  A @Override the      Public voidOnClick (View v) { +         if(V.equals (Btnlow)) { -Vibrator.vibrate (New Long[] {1000,30,100,1000},0); $         } $         if(V.equals (btnnormal)) { -              -Vibrator.vibrate (New Long[] {800,40,400,30},0); the         } -         if(V.equals (btnfast)) {WuyiVibrator.vibrate (New Long[] {30,100,20,50},0); the         } -  Wu     } -  About protected voidOnStop () { $         if(NULL!=vibrator) { - Vibrator.cancel (); -         } -         Super. OnStop (); A     } +}

2. It was only later that the original question was so simple that the revised code was as follows:

1  Packagecom.himi.vibrate;2 3 Importandroid.app.Activity;4 ImportAndroid.app.Service;5 ImportAndroid.content.res.AssetManager;6 ImportAndroid.graphics.Typeface;7 ImportAndroid.os.Bundle;8 ImportAndroid.os.Vibrator;9 ImportAndroid.view.View;Ten ImportAndroid.view.View.OnClickListener; One ImportAndroid.widget.Button; A  -  Public classMainactivityextendsActivityImplementsOnclicklistener { -     PrivateButton Btnlow; the     PrivateButton Btnnormal; -     PrivateButton btnfast; -      - Vibrator Vibrator; +  - @Override +      Public voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -          -Vibrator =(Vibrator) Getsystemservice (service.vibrator_service); -          -Btnlow =(Button) Findviewbyid (r.id.btnlow); -Btnnormal =(Button) Findviewbyid (r.id.btnnormal); inBtnfast =(Button) Findviewbyid (r.id.btnfast); -          toAssetmanager mgr =getassets (); +Typeface tf = Typeface.createfromasset (Mgr, "Fonts/droidsansfallback.ttf"); -          the Btnlow.settypeface (TF); * Btnnormal.settypeface (TF); $ Btnfast.settypeface (TF);Panax Notoginseng          -         //before adding these 3 statements, the vibration service is unresponsive. And then I found out it was a grammar, and I was a jerk.  theBtnfast.setonclicklistener ( This);//monitor current mainactivity +Btnnormal.setonclicklistener ( This);//monitor current mainactivity ABtnlow.setonclicklistener ( This);//monitor current mainacitivity the          +     } -  $ @Override $      Public voidOnClick (View v) { -         if(V.equals (Btnlow)) { -Vibrator.vibrate (New Long[] {1000,30,100,1000},0); the         } -         if(V.equals (btnnormal)) {Wuyi              theVibrator.vibrate (New Long[] {800,40,400,30},0); -         } Wu         if(V.equals (btnfast)) { -Vibrator.vibrate (New Long[] {30,100,20,50},0); About         } $  -     } -  -      A     protected voidOnStop () { +         if(NULL!=vibrator) { the Vibrator.cancel (); -         } $         Super. OnStop (); the     } the}

3. Summary

Here is the mainacitivity implementation of the Onclicklistener interface, so the interface monitoring function is the current mainactivity, so is to listen to the current mainactivity:

The syntax format is as follows:

public class Mainactivity extends Activity implements Onclicklistener {

..................

public void OnCreate (Bundle savedinstancestate) {

............

btn1 = (Button) Findviewbyid (r.id.button1);

Btn1.setonclicklistener (this); listen to the current acitivity, of course, this is mainactivity.

}

Public void OnClick (View v) {

if (V.equals (BTN1)) {

............

}

........................

}

Android (Java) Learning Note 113:android Write code call vibrator Vibrate function, press button buttons always not vibrate

Related Article

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.