本文實(shí)例講述了Android實(shí)現(xiàn)手機(jī)振動(dòng)設(shè)置的方法。分享給大家供大家參考。具體如下:
main.xml布局文件:
<?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="關(guān)閉振動(dòng)" android:textOff="啟動(dòng)振動(dòng)" android:checked="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:text="振動(dòng)已關(guān)閉" 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="關(guān)閉振動(dòng)" android:textOff="啟動(dòng)振動(dòng)" android:checked="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:text="振動(dòng)已關(guān)閉" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
清單文件:
<?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" /> <!-- 設(shè)置手機(jī)震動(dòng)權(quán)限 --> <uses-permission android:name="android.permission.VIBRATE" /></manifest>
VibrateActivity類:
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); //注意模擬器是模擬不了震動(dòng)的,得真機(jī)測(cè)試哦 //創(chuàng)建vibrator對(duì)象 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){ //根據(jù)指定的模式進(jìn)行震動(dòng) //第一個(gè)參數(shù):該數(shù)組中第一個(gè)元素是等待多長的時(shí)間才啟動(dòng)震動(dòng), //之后將會(huì)是開啟和關(guān)閉震動(dòng)的持續(xù)時(shí)間,單位為毫秒 //第二個(gè)參數(shù):重復(fù)震動(dòng)時(shí)在pattern中的索引,如果設(shè)置為-1則表示不重復(fù)震動(dòng) vibrator.vibrate(new long[]{1000,50,50,100,50}, -1); tv1.setText("振動(dòng)已啟動(dòng)"); }else { //關(guān)閉震動(dòng) vibrator.cancel(); tv1.setText("震動(dòng)已關(guān)閉"); } break; case R.id.tb2: if(isChecked){ //啟動(dòng)震動(dòng),并持續(xù)指定的時(shí)間 vibrator.vibrate(3500); tv2.setText("振動(dòng)已啟動(dòng)"); }else { //關(guān)閉啟動(dòng) vibrator.cancel(); tv2.setText("震動(dòng)已關(guān)閉"); } break; } } };}運(yùn)行結(jié)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選