Android中手機震動的設定(Vibrator)的步驟(Android學習隨筆三)

來源:互聯網
上載者:User

 

Android中手機震動的設定(Vibrator)的步驟:

 

a、通過系統服務獲得手機震動服務,Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);  

 

b、得到震動服務後檢測vibrator是否存在:

 

vibrator.hasVibrator();

 

檢測當前硬體是否有vibrator,如果有返回true,如果沒有返回false。

 

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

 

vibrator.vibrate(long milliseconds);

 

開始啟動vibrator持續milliseconds毫秒。     

 

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

 

以pattern方式重複repeat次啟動vibrator。(pattern的形式為new  long[]{arg1,arg2,arg3,arg4......},其中以兩個一組的如arg1 和arg2為一組、arg3和arg4為一組,每一組的前一個代表等待多少毫 秒啟動vibrator,後一個代表vibrator持續多少毫秒停止,之後往複即 可。Repeat表示重複次數,當其為-1時,表示不重複只以pattern的方 式運行一次)。

 

vibrator.cancel();

 

Vibrator停止。

 

注意:在使用震動服務時需要如下許可權:android.permission.VIBRATE

 


摘自 tianshijianbing1989的專欄

  

聯繫我們

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