The method of Android to realize the vibration setting of mobile phone _android

Source: Internet
Author: User

This paper illustrates the method of Android to realize the vibration setting of mobile phone. Share to everyone for your reference. Specifically as follows:

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <linearlayout android:orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height=
      "Wrap_content" > <togglebutton android:id= "@+id/tb1" android:texton= "Turn off vibration" android:textoff= "Start Vibration"
    Android:checked= "false" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <textview android:id= "@+id/tv1" android:text= "Vibration closed" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content"/> </LinearLayout> <linearlayout android:orientation= "Horizontal" Android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" > <togglebutton android:id= "@+id/tb2" a ndroid:texton= "Off Vibration" Android:textoff= "Start Vibration" android:checked= "false" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_conte
      NT "/> <textview android:id=" @+id/tv2 "android:text=" "Vibration closed" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> </LinearLayout> </LinearLayout>

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  package=" com.ljq.activity "android:versioncode=" 1 "
  android:versionname=" 1.0 ">
  < Application android:icon= "@drawable/icon"
    android:label= "@string/app_name" >
    <activity android:name =". Vibrateactivity "
      android:label=" @string/app_name ">
      <intent-filter>
        <action android:name = "Android.intent.action.MAIN"/>
        <category
          android:name= "Android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android: minsdkversion= "7"/>
  <!--set mobile phone vibration permission-->
  <uses-permission android:name= " Android.permission.VIBRATE "/>
</manifest>

Vibrateactivity class:

Package com.ljq.activity;
Import android.app.Activity;
Import Android.app.Service;
Import Android.os.Bundle;
Import Android.os.Vibrator;
Import Android.widget.CompoundButton;
Import Android.widget.TextView;
Import Android.widget.ToggleButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
  public class Vibrateactivity extends activity {private vibrator vibrator=null;
  Private ToggleButton tb1=null, Tb2=null;
  Private TextView tv1=null, Tv2=null;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Note that the simulator is not analog vibration, get the real machine test OH//Create Vibrator Object vibrator= (Vibrator) Getsystemservice (Service.vibrator_service);
    tv1= (TextView) Findviewbyid (R.ID.TV1);
    Tv2= (TextView) Findviewbyid (R.ID.TV2);
    tb1= (ToggleButton) Findviewbyid (R.ID.TB1);
    Tb2= (ToggleButton) Findviewbyid (R.ID.TB2);
    Tb1.setoncheckedchangelistener (listener);
  Tb2.setoncheckedchangelistener (listener); } onchEckedchangelistener listener=new Oncheckedchangelistener () {public void oncheckedchanged (Compoundbutton ButtonView, b
      Oolean ischecked) {ToggleButton togglebutton= (ToggleButton) Buttonview; Switch (Togglebutton.getid ()) {case R.id.tb1:if (ischecked) {//vibration based on the specified pattern///First parameter: in the array The first element is how long it will take to start the vibration,//And the duration of the vibration to be turned on and off, in milliseconds/the second parameter: The index of the pattern in the case of repeated vibrations, if set to 1, which means no repetition of the vibration VIBR
          Ator.vibrate (New long[]{1000,50,50,100,50},-1);
        Tv1.settext ("Vibration has started");
          }else {//close vibration vibrator.cancel ();
        Tv1.settext ("Vibration has been closed");
      } break;
          Case R.id.tb2:if (ischecked) {//Start vibration, and continue to specify the time vibrator.vibrate (3500);
        Tv2.settext ("Vibration has started");
          }else {//Shutdown start vibrator.cancel ();
        Tv2.settext ("Vibration has been closed");
      } break;
}
    }
  };

 }

Run Result:

I hope this article will help you with your Android program.

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.