Android振動器使用方法詳解
本文實例為大家分享了Android振動器使用方法的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

選擇相應(yīng)的毫秒數(shù),就會振動相應(yīng)的秒數(shù)。
實現(xiàn)步驟:
一、創(chuàng)建activity_vibrator.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VibratorActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="振動時長:"
android:textSize="15sp"
android:textColor="@color/black"
android:paddingLeft="5dp"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:paddingTop="5dp"
android:spinnerMode="dialog"
/>
</LinearLayout>
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始振動"
android:textColor="@color/black"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv_specific"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:text="當(dāng)前振動了多長時間"
/>
</LinearLayout>
之后繪制,下拉列表,每一列的高度和每一列中字體的顏色和太小等屬性在這里面設(shè)置
item_select.xml布局如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="20sp" />
之后在VibratorActivity中實現(xiàn)振動功能:
public class VibratorActivity extends AppCompatActivity implements View.OnClickListener {
private Spinner spinner;
private TextView tv_specific;
private Button btn_start;
private ArrayAdapter<String> arrayAdapter;
private String second;
private Vibrator vibrator;
private int mDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrator);
spinner = findViewById(R.id.spinner);
tv_specific = findViewById(R.id.tv_specific);
btn_start = findViewById(R.id.btn_start);
//設(shè)置下拉框
CreateSpinner();
btn_start.setOnClickListener(this);
}
private void CreateSpinner() {
String[] array = new String[]{"0.5秒", "1秒", "2秒", "3秒", "4秒", "5秒"};
int[] durationArray = new int[]{500, 1000, 2000, 3000, 4000, 5000};
//設(shè)置我們自定義的資源樣式
arrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, array);
spinner.setPrompt("請選擇毫秒數(shù)");
//將適配器與下拉列表框關(guān)聯(lián)起來
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mDuration = durationArray[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onClick(View v) {
String vibratorService = Context.VIBRATOR_SERVICE;
//從系統(tǒng)服務(wù)中獲取振動管理器
vibrator = (Vibrator) getSystemService(vibratorService);
//判斷設(shè)置是否包含振動器
if (vibrator.hasVibrator()) {
//振動的秒數(shù)
vibrator.vibrate(mDuration);
String desc = String.format("%s手機振動了%f秒", DateUtil.getNowTimeDetail(), mDuration / 1000.0F);
tv_specific.setText(desc);
}
}
//應(yīng)用退出,則取消振動
@Override
protected void onDestroy() {
super.onDestroy();
vibrator.cancel();
}
}
最后不要忘了在AndroidManifest.xml清單文件中加入控制設(shè)備振動的權(quán)限:
<!-- 振動權(quán)限 --> <uses-permission android:name="android.permission.VIBRATE" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中Xposed框架篇---修改系統(tǒng)位置信息實現(xiàn)自身隱藏功能實例
本篇文章介紹了Android中Xposed框架的使用,詳細(xì)的介紹了修改系統(tǒng)位置信息實現(xiàn)自身隱藏功能實例,有需要的朋友可以了解一下。2016-11-11
詳解Flutter?響應(yīng)式狀態(tài)管理框架GetX
這篇文章主要為大家介紹了Flutter?響應(yīng)式狀態(tài)管理框架GetX詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
利用kotlin實現(xiàn)統(tǒng)計文件字符個數(shù)的方法示例
最近在學(xué)習(xí)kotlin,發(fā)現(xiàn)了一些不錯的小技巧,所以下面這篇文章主要給大家介紹了關(guān)于利用kotlin實現(xiàn)統(tǒng)計文件字符個數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

