Android实现手机振动设置的方法
作者:Ruthless 发布时间:2021-08-02 23:46:21
标签:Android,振动,设置
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 详解java 中Spring jsonp 跨域请求的实例jsonp介绍  
- 前言借用《Effactive Java》这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点
- 实现效果如图所示:首先公布实现代码:一. 自定义实现import.org.springframework.security.core.use
- 本文实例讲述了C#实现系统托盘通知的方法。分享给大家供大家参考。具体实现方法如下:namespace WindowsApplication1
- 1、首先,找到 Android SDK 在本机中的位置,如果不知道,可以通过在 Android Studio 找到,如下:2、其次,通过 c
- 闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有任何线程可以通过,当到达结束状态时,这扇门才会打开并容许所有线程通过。它
- hystrixDashboard服务监控除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboar
- 一、新建BeanUtil类import lombok.extern.slf4j.Slf4j;import org.springframewo
- 一、基本介绍(Nexus(maven * ))1,如果没有搭建 * 会有什么问题?如果没有 * ,我们所需的所有构件都需要通过 Mave
- 其实SynchronousQueue 是一个特别有意思的阻塞队列,就我个人理解来说,它很重要的特点就是没有容量。直接看一个例子:packag
- 今天写项目突然出现了无法启动Gradle的bug,如下图然后就看了log日志:这个问题是我第一次看见,然后就开始了各种百度,有说需要在And
- 最近有个粉丝提了个问题,说他在Spring Security中用JWT做退出登录的时无法获取当前用户,导致无法证明“我就是要退出的那个我”,
- 1. 引言 * (Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter。我们可以让普通的B
- 简单介绍一下Java中的Excel文件导出功能(基于HttpServletResponse实现下载)首先,引入需要依赖的jar包:<d
- 界面开发中,经常使用观察者设计模式来实现文档/视图模式,当文档内容改变时,作为观察者的用户视图必须相应作出调整以向用户呈现文档的状态。由于语
- malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的
- 解释:二叉树的深度:从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。二叉树的宽度:二叉树的每一层中
- 最近自己写了个小爬虫,里面用到了多线程技术,忽然发现对此技术竟然有些陌生了,于是乎开始疯狂的去问度娘,在此记录下来,以便自己和各位小伙伴们学
- 依赖<dependency> <groupId>io.springfox</groupId> <a
- 本文实例为大家分享了Java实现统计字符串出现次数的具体代码,供大家参考,具体内容如下需求:健盘录入一个字符串,要求统计字符串中每个字符串出