使用Vibrator類寫安卓震動詳細步驟

來源:互聯網
上載者:User

標籤:android   style   io   使用   sp   檔案   on   代碼   bs   

在AndroidMainfest.xml檔案中聲明震動許可權

<uses-permission android:name=”android.permission.VIBRATE”/>


通過系統服務獲得手機震動服務,如

Vibrator vibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE)

得到震動服務後檢測vibrator是否存在,如

vibrator.hasVibrator();

如果有返回true,否則返回false

 

根據實際需要進行適當的調用,如

Vibrator.vibrator(long milliseconds);

通過上述代碼開始啟動vibrator持續milliseconds毫秒(1000毫秒=1秒)

 

Vibrator.vibrator(long[] pattern, int repeat);

這樣以pattern方式repeat(重複)啟動vibrator(震動)。使用pattern的形式如下:

new long[]{a1,a2,a3,a4......}

上述格式中,其中以兩個一組,如a1和a2為一組、a3和a4為一組,每一組的前一個代表等待多少毫秒啟動vibrator,後一個代表vibrator持續多少毫秒停止,之後往複即可。Repeat表示重複次數,當其為-1時,表示不重複只以pattern的方式運行一次


停止震動

Vibrator.cancel();

使用Vibrator類寫安卓震動詳細步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.