Android入門教程之Vibrator(振動(dòng)器)
前言:
Vibrator簡(jiǎn)介:
下面我們就來(lái)寫個(gè)簡(jiǎn)單的例子,來(lái)熟悉下這個(gè)Vibrator的用法!
1.獲得Vibrator實(shí)例:
Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
2.可以使用的相關(guān)方法:
1.stract void cancel():關(guān)閉或者停止振動(dòng)器
2.tract boolean hasVibrator():判斷硬件是否有振動(dòng)器
3.id vibrate(long milliseconds):控制手機(jī)振動(dòng)為milliseconds毫秒
4.id vibrate(long[] pattern,int repeat):指定手機(jī)以pattern指定的模式振動(dòng)! 比如:pattern為new int[200,400,600,800],就是讓他在200,400,600,800這個(gè)時(shí)間交替啟動(dòng)與關(guān)閉振動(dòng)器! 而第二個(gè)則是重復(fù)次數(shù),如果是-1的只振動(dòng)一次,如果是0的話則一直振動(dòng) 還有其他兩個(gè)方法用得不多~ 對(duì)了,使用振動(dòng)器還需要在AndroidManifest.xml中添加下述權(quán)限: <uses-permission android:name="android.permission.VIBRATE"/>
3.使用示例:設(shè)置頻率不同的震動(dòng)器:
對(duì)于Vibrator用的最廣泛的莫過(guò)于所謂的手機(jī)按摩器類的app,在app市場(chǎng)一搜,一堆,筆者隨便下了 幾個(gè)下來(lái)瞅瞅,都是大同小異的,這點(diǎn)小玩意竟然有8W多的下載量...好吧,好像也不算多, 不過(guò)普遍功能都是切換振動(dòng)頻率來(lái)完成,而所謂的按摩效果,是否真的有效就不得而知了, 那么接下來(lái)我們就來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的按摩器吧! 核心其實(shí)就是:vibrate()中的數(shù)組的參數(shù),根據(jù)自己需求寫一個(gè)數(shù)組就可以了! 下述代碼需要在真機(jī)上進(jìn)行測(cè)試!
運(yùn)行效果圖:
實(shí)現(xiàn)代碼:
簡(jiǎn)單的布局文件,五個(gè)按鈕:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_hasVibrator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="判斷是否有振動(dòng)器" /> <Button android:id="@+id/btn_short" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短振動(dòng)" /> <Button android:id="@+id/btn_long" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="長(zhǎng)振動(dòng)" /> <Button android:id="@+id/btn_rhythm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="節(jié)奏振動(dòng)" /> <Button android:id="@+id/btn_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消振動(dòng)" /> </LinearLayout>
接著是MainActivity.java部分:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_hasVibrator; private Button btn_short; private Button btn_long; private Button btn_rhythm; private Button btn_cancle; private Vibrator myVibrator; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲得系統(tǒng)的Vibrator實(shí)例: myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); mContext = MainActivity.this; bindViews(); } private void bindViews() { btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator); btn_short = (Button) findViewById(R.id.btn_short); btn_long = (Button) findViewById(R.id.btn_long); btn_rhythm = (Button) findViewById(R.id.btn_rhythm); btn_cancle = (Button) findViewById(R.id.btn_cancle); btn_hasVibrator.setOnClickListener(this); btn_short.setOnClickListener(this); btn_long.setOnClickListener(this); btn_rhythm.setOnClickListener(this); btn_cancle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_hasVibrator: Toast.makeText(mContext, myVibrator.hasVibrator() ? "當(dāng)前設(shè)備有振動(dòng)器" : "當(dāng)前設(shè)備無(wú)振動(dòng)器", Toast.LENGTH_SHORT).show(); break; case R.id.btn_short: myVibrator.cancel(); myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0); Toast.makeText(mContext, "短振動(dòng)", Toast.LENGTH_SHORT).show(); break; case R.id.btn_long: myVibrator.cancel(); myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0); Toast.makeText(mContext, "長(zhǎng)振動(dòng)", Toast.LENGTH_SHORT).show(); break; case R.id.btn_rhythm: myVibrator.cancel(); myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0); Toast.makeText(mContext, "節(jié)奏振動(dòng)", Toast.LENGTH_SHORT).show(); break; case R.id.btn_cancle: myVibrator.cancel(); Toast.makeText(mContext, "取消振動(dòng)", Toast.LENGTH_SHORT).show(); } } }
對(duì)了,別漏了振動(dòng)器權(quán)限哦!
<uses-permission android:name="android.permission.VIBRATE"/>
小結(jié):
好了,本文我們學(xué)習(xí)了Vibrator(振動(dòng)器)的基本使用,代碼非常簡(jiǎn)單,還不趕緊加入到 你的APP中,讓你的應(yīng)用HI起來(lái)~
相關(guān)文章
Android利用Intent啟動(dòng)和關(guān)閉Activity
這篇文章主要為大家詳細(xì)介紹了Android利用Intent啟動(dòng)和關(guān)閉Activity的相關(guān)操作,感興趣的小伙伴們可以參考一下2016-06-06Android 三種動(dòng)畫詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法,涉及Android StatusBarManager相關(guān)屬性控制操作技巧,需要的朋友可以參考下2017-08-08ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)
本文給大家分享一段實(shí)例代碼給大家介紹ImageView點(diǎn)擊可變暗的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-02-02Android基礎(chǔ)知識(shí)之broadcast廣播詳解
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)知識(shí)之broadcast廣播的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android clipChildren屬性實(shí)例詳解
本文主要介紹Android clipChildren的屬性,這里對(duì)clipChildren屬性做了一個(gè)小例子,展示了效果圖和實(shí)例代碼,方便大家觀看理解2016-07-07淺談Android為RecyclerView增加監(jiān)聽(tīng)以及數(shù)據(jù)混亂的小坑
下面小編就為大家?guī)?lái)一篇淺談Android為RecyclerView增加監(jiān)聽(tīng)以及數(shù)據(jù)混亂的小坑。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android?Xml轉(zhuǎn)換為View過(guò)程詳解
這篇文章主要為大家介紹了Android?Xml轉(zhuǎn)換為View實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07