Home>

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 is off"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </linearlayout>
  <linearlayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <togglebutton android:id="@ + id/tb2"
      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/tv2"
      android:text="Vibration is off"
      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 phone vibration permissions->
  <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 cannot simulate vibration,Real machine test
    //Create a 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, boolean ischecked) {
      togglebutton togglebutton=(togglebutton) buttonview;
      switch (togglebutton.getid ()) {
      case r.id.tb1:
        if (ischecked) {
          //Vibrate according to the specified pattern
          //First parameter:how long does the first element in the array wait to start the vibration,          //After that will be the duration of the on and off vibrations,Unit is millisecond
          //The second parameter:the index in the pattern when the vibration is repeated,If set to -1, it means no repeated vibration
          vibrator.vibrate (new long [] {1000,50,50,100,50}, -1);
          tv1.settext ("Vibration has started");
        } else {
          //turn off the vibration
          vibrator.cancel ();
          tv1.settext ("Vibration is off");
        }
        break;
      case r.id.tb2:
        if (ischecked) {
          //Start the vibration,For the specified time
          vibrator.vibrate (3500);
          tv2.settext ("Vibration has started");
        } else {
          //Close and start
          vibrator.cancel ();
          tv2.settext ("Vibration is off");
        }
        break;
      }
    }
  };
}

operation result:

  • Previous How ASPNET file processing works
  • Next PHP product comparison function code sharing