效果图:
选择相应的毫秒数,就会振动相应的秒数。
实现步骤:
一、创建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="当前振动了多长时间" /> </LinearLayout>
之后绘制,下拉列表,每一列的高度和每一列中字体的颜色和大小等属性在这里面设置
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中实现振动功能:
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); //设置下拉框 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}; //设置我们自定义的资源样式 arrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, array); spinner.setPrompt("请选择毫秒数"); //将适配器与下拉列表框关联起来 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; //从系统服务中获取振动管理器 vibrator = (Vibrator) getSystemService(vibratorService); //判断设置是否包含振动器 if (vibrator.hasVibrator()) { //振动的秒数 vibrator.vibrate(mDuration); String desc = String.format("%s手机振动了%f秒", DateUtil.getNowTimeDetail(), mDuration / 1000.0F); tv_specific.setText(desc); } } //应用退出,则取消振动 @Override protected void onDestroy() { super.onDestroy(); vibrator.cancel(); } }
最后不要忘了在AndroidManifest.xml清单文件中加入控制设备振动的权限:
<!-- 振动权限 --> <uses-permission android:name="android.permission.VIBRATE" />