Android实现手机振动设置的方法
作者:Ruthless 时间:2021-08-02 23:46:21
本文实例讲述了Android实现手机振动设置的方法。分享给大家供大家参考。具体如下:
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton android:id="@+id/tb1"
android:textOn="关闭振动"
android:textOff="启动振动"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tv1"
android:text="振动已关闭"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ToggleButton android:id="@+id/tb2"
android:textOn="关闭振动"
android:textOff="启动振动"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tv2"
android:text="振动已关闭"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".VibrateActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<!-- 设置手机震动权限 -->
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
VibrateActivity类:
package com.ljq.activity;
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class VibrateActivity extends Activity {
private Vibrator vibrator=null;
private ToggleButton tb1=null, tb2=null;
private TextView tv1=null, tv2=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//注意模拟器是模拟不了震动的,得真机测试哦
//创建vibrator对象
vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tb1=(ToggleButton)findViewById(R.id.tb1);
tb2=(ToggleButton)findViewById(R.id.tb2);
tb1.setOnCheckedChangeListener(listener);
tb2.setOnCheckedChangeListener(listener);
}
OnCheckedChangeListener listener=new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ToggleButton toggleButton=(ToggleButton)buttonView;
switch (toggleButton.getId()) {
case R.id.tb1:
if(isChecked){
//根据指定的模式进行震动
//第一个参数:该数组中第一个元素是等待多长的时间才启动震动,
//之后将会是开启和关闭震动的持续时间,单位为毫秒
//第二个参数:重复震动时在pattern中的索引,如果设置为-1则表示不重复震动
vibrator.vibrate(new long[]{1000,50,50,100,50}, -1);
tv1.setText("振动已启动");
}else {
//关闭震动
vibrator.cancel();
tv1.setText("震动已关闭");
}
break;
case R.id.tb2:
if(isChecked){
//启动震动,并持续指定的时间
vibrator.vibrate(3500);
tv2.setText("振动已启动");
}else {
//关闭启动
vibrator.cancel();
tv2.setText("震动已关闭");
}
break;
}
}
};
}
运行结果:
希望本文所述对大家的Android程序设计有所帮助。
标签:Android,振动,设置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
实例讲解Java的Spring框架中的控制反转和依赖注入
2023-04-24 17:44:49
![](https://img.aspxhome.com/file/2023/8/76228_0s.jpg)
Java注解之Elasticsearch的案例详解
2022-03-30 12:28:58
Java IO之File 类详解
2023-08-07 20:02:12
![](https://img.aspxhome.com/file/2023/2/57802_0s.png)
OpenCV图像旋转Rotate的详细介绍
2023-07-01 08:22:27
![](https://img.aspxhome.com/file/2023/2/95522_0s.jpg)
Spring Boot如何整合FreeMarker模板引擎
2022-09-06 15:49:32
花样使用Handler与源码分析
2023-07-30 08:36:31
Java 使用Docker时经常遇到的五个问题
2023-12-10 07:20:21
![](https://img.aspxhome.com/file/2023/7/79317_0s.jpg)
彻底搞懂Java多线程(一)
2023-08-02 10:42:30
![](https://img.aspxhome.com/file/2023/2/71632_0s.png)
深入c# Func委托的详解
2022-02-15 05:44:51
Spring Cloud升级最新Finchley版本的所有坑
2021-09-02 07:21:51
![](https://img.aspxhome.com/file/2023/3/88593_0s.png)
关于通过java调用datax,返回任务执行的方法
2023-11-28 21:26:45
![](https://img.aspxhome.com/file/2023/1/60381_0s.jpg)
Java实现经典游戏黄金矿工的示例代码
2022-01-09 16:35:41
![](https://img.aspxhome.com/file/2023/2/79222_0s.jpg)
java实现摄像头截图功能
2023-12-01 19:53:22
基于Mybatis-Plus的CRUD的实现
2023-09-10 14:38:45
IDEA无法使用Git Pull的问题
2023-05-04 10:55:39
![](https://img.aspxhome.com/file/2023/5/79945_0s.png)
解决Mybatis的@Param()注解导致分页失效的问题
2022-05-01 13:24:53
![](https://img.aspxhome.com/file/2023/0/63400_0s.jpg)
Android WebView实现顶部进度条
2023-10-14 23:44:03
![](https://img.aspxhome.com/file/2023/1/88731_0s.jpg)
idea如何设置Git忽略对某些文件或文件夹的版本追踪
2021-08-24 02:58:15
![](https://img.aspxhome.com/file/2023/0/83080_0s.jpg)
C#中的TemplateMethod模式问题分析
2021-12-01 04:02:44
![](https://img.aspxhome.com/file/2023/3/79103_0s.png)
Android EditText限制输入字符类型的方法总结
2023-10-11 15:48:19