Android入門教程之Vibrator(振動器)
前言:
Vibrator簡介:

下面我們就來寫個簡單的例子,來熟悉下這個Vibrator的用法!
1.獲得Vibrator實例:
Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
2.可以使用的相關(guān)方法:
1.stract void cancel():關(guān)閉或者停止振動器
2.tract boolean hasVibrator():判斷硬件是否有振動器
3.id vibrate(long milliseconds):控制手機振動為milliseconds毫秒
4.id vibrate(long[] pattern,int repeat):指定手機以pattern指定的模式振動! 比如:pattern為new int[200,400,600,800],就是讓他在200,400,600,800這個時間交替啟動與關(guān)閉振動器! 而第二個則是重復(fù)次數(shù),如果是-1的只振動一次,如果是0的話則一直振動 還有其他兩個方法用得不多~ 對了,使用振動器還需要在AndroidManifest.xml中添加下述權(quán)限: <uses-permission android:name="android.permission.VIBRATE"/>
3.使用示例:設(shè)置頻率不同的震動器:
對于Vibrator用的最廣泛的莫過于所謂的手機按摩器類的app,在app市場一搜,一堆,筆者隨便下了 幾個下來瞅瞅,都是大同小異的,這點小玩意竟然有8W多的下載量...好吧,好像也不算多, 不過普遍功能都是切換振動頻率來完成,而所謂的按摩效果,是否真的有效就不得而知了, 那么接下來我們就來實現(xiàn)一個簡單的按摩器吧! 核心其實就是:vibrate()中的數(shù)組的參數(shù),根據(jù)自己需求寫一個數(shù)組就可以了! 下述代碼需要在真機上進(jìn)行測試!
運行效果圖:

實現(xiàn)代碼:
簡單的布局文件,五個按鈕: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="判斷是否有振動器" />
<Button
android:id="@+id/btn_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短振動" />
<Button
android:id="@+id/btn_long"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="長振動" />
<Button
android:id="@+id/btn_rhythm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="節(jié)奏振動" />
<Button
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消振動" />
</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實例:
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)前設(shè)備無振動器",
Toast.LENGTH_SHORT).show();
break;
case R.id.btn_short:
myVibrator.cancel();
myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);
Toast.makeText(mContext, "短振動", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_long:
myVibrator.cancel();
myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
Toast.makeText(mContext, "長振動", 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é)奏振動", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_cancle:
myVibrator.cancel();
Toast.makeText(mContext, "取消振動", Toast.LENGTH_SHORT).show();
}
}
}
對了,別漏了振動器權(quán)限哦!
<uses-permission android:name="android.permission.VIBRATE"/>
小結(jié):
好了,本文我們學(xué)習(xí)了Vibrator(振動器)的基本使用,代碼非常簡單,還不趕緊加入到 你的APP中,讓你的應(yīng)用HI起來~
相關(guān)文章
Android實現(xiàn)回彈ScrollView的原理
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)回彈ScrollView的原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android監(jiān)聽ScrollView滑動距離的簡單處理
這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽ScrollView滑動距離的簡單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
JetPack Compose底部導(dǎo)航欄的實現(xiàn)方法詳解
開發(fā)一個新項目,底部導(dǎo)航欄一般是首頁的標(biāo)配,在以前的xml布局中,我們可以很輕松的是用谷歌提供的BottomNavigationView或者自定義來實現(xiàn)底部導(dǎo)航的功能,在Compose中也有也提供了一個類似的控件androidx.compose.material.BottomNavigation2022-09-09
Android實現(xiàn)點擊獲取驗證碼60秒后重新獲取功能
這篇文章主要為大家詳細(xì)介紹了Android點擊獲取驗證碼60秒后重新獲取驗證碼的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實現(xiàn)在類里面創(chuàng)建main函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android之在linux終端執(zhí)行shell腳本直接打印當(dāng)前運行app的日志的實現(xiàn)方法
今天小編就為大家分享一篇關(guān)于Android之在linux終端執(zhí)行shell腳本直接打印當(dāng)前運行app的日志的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02

