android-Vibrator的使用

來源:互聯網
上載者:User

android手機中的震動由Vibrator實現。設定震動事件,需要知道其震動的時間長短、震動的周期等。

在android中,震動的時間一毫秒計算(1/1000秒),所以如果設定的時間值太小,會感覺不出來。

通過調用Vibrator的vibrate(long[] pattern, int repeat)方法實現。

前一個參數為設定震動的效果的數組,第二個參數為 -1表示只震動一次,為0則震動會一直持續。

一個demo:

package com.shao.vibrator;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.os.Vibrator;<br />import android.widget.CompoundButton;<br />import android.widget.Toast;<br />import android.widget.CompoundButton.OnCheckedChangeListener;<br />import android.widget.ToggleButton;</p><p>public class VibratorActivity extends Activity {<br /> /** Called when the activity is first created. */</p><p>private Vibrator vibrator;<br />private ToggleButton tog1;<br />private ToggleButton tog2;<br />private ToggleButton tog3;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> init();<br /> tog1.setOnCheckedChangeListener(new OnCheckedChangeListener() {</p><p>@Override<br />public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<br />// TODO Auto-generated method stub<br />if(isChecked){<br />//設定震動周期<br />vibrator.vibrate(new long[]{1000,10,100,1000}, -1);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<br />}<br />});<br /> tog2.setOnCheckedChangeListener(new OnCheckedChangeListener() {</p><p>@Override<br />public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<br />// TODO Auto-generated method stub<br />if(isChecked){<br />//設定震動周期<br />vibrator.vibrate(new long[]{100,100,100,1000}, 0);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<br />}<br />});<br /> tog3.setOnCheckedChangeListener(new OnCheckedChangeListener() {</p><p>@Override<br />public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<br />// TODO Auto-generated method stub<br />if(isChecked){<br />//設定震動周期<br />vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);<br />showToast("OK");<br />}else{<br />//取消震動<br />vibrator.cancel();<br />showToast("CANCEL");<br />}<br />}<br />});</p><p> }<br /> private void init(){<br /> tog1= (ToggleButton) findViewById(R.id.tog1);<br /> tog2= (ToggleButton) findViewById(R.id.tog2);<br /> tog3= (ToggleButton) findViewById(R.id.tog3);<br /> vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);<br /> }<br /> private void showToast(String msg){<br /> Toast.makeText(this, msg, 1).show();<br /> }<br />}
xml:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <RelativeLayout<br /> android:layout_marginTop="20dp"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="短震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><br /> </RelativeLayout><br /> <RelativeLayout<br /> android:layout_marginTop="20dp"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content" </p><p> android:text="長震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><br /> </RelativeLayout><br /> <RelativeLayout<br /> android:layout_marginTop="20dp"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content" </p><p> android:text="節奏震動"<br /> /><br /><ToggleButton<br />android:id="@+id/tog3"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:textOn="關閉"<br /> android:textOff="開啟"<br /> android:layout_alignParentRight="true"<br /> /><br /> </RelativeLayout><br /></LinearLayout><br />

最後別忘了加上   <uses-permission  android:name="android.permission.VIBRATE"/>許可權

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.