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程序设计有所帮助。


猜你喜欢
- 整合Spring Data JPAJPA (Java Persistence API)和 Spring Data 是两个范畴的概念。Hibe
- Java程序有的时候在主线程中会创建多个线程去执行任务,然后在主线程执行完毕之前,把所有线程的任务进行汇总,以前可以用线程的join方法,但
- Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再
- 1、导入资源2、JSP代码<div class="page-container">  
- 无限滚动复用列表Demo展示前言游戏中有非常多的下拉滚动菜单,比如成就列表,任务列表,以及背包仓库之类;如果列表内容非常丰富,会占用大量内存
- 我就废话不多说了,大家还是直接看代码吧~ public List<FreightM> sortList(List&l
- 一、申请你的AppIDhttp://open.weixin.qq.com/ 友情提示:推荐使用eclipse打包软件最后一步的M
- 数据表及数据准备:create table Member(MemberId int primary key identity(1,1),Me
- 背景今天面试字节算法岗时被问到的问题,让我用C++实现一个softmax函数。softmax是逻辑回归在多分类问题上的推广。大概的公式如下:
- 很多时候我们复制一个对象实例A到实例B,在用实例B去做其他事情的时候,会对实例B进行修改,为保证对B的修改不会影响到A的正常使用,就需要使用
- 前言当线程池的线程阻塞时,线程池会创建额外的线程,而创建、销毁和调度线程所需要相当昂贵的内存资源,另外,很多的开发人员看见自己程序的线程没有
- 本文实例讲述了C#实现HTTP上传文件的方法。分享给大家供大家参考。具体实现方法如下:发送文件代码如下:/// <summary>
- 启用开发者模式①填写服务器配置启用开发模式需要先成为开发者,而且编辑模式和开发模式只能选择一个(进入微信公众平台=>开发=>基本
- 本文实例为大家分享了C++实现希尔排序的具体代码,供大家参考,具体内容如下一、思路:希尔排序:又称缩小增量排序,是一种改进的插入排序算法,是
- 1. 为什么要使用线程池使用线程池通常由以下两个原因:频繁创建销毁线程需要消耗系统资源,使用线程池可以复用线程。使用线程池可以更容易管理线程
- 我们首先看下BASEJDBC的写法实例:package com.dao;import java.sql.Connection;import
- 一、概述近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下效果图:恩,接下来正题。首先只有大于等于4.4版本支持这个半透明状态栏的效果,
- 在本篇介绍的Winform界面样式改变及存储操作中,是指基于DevExpress进行界面样式的变化。一般情况下,默认我们会为客户提供多种De
- Java的常量池通常分为两种:静态常量池和运行时常量池静态常量池:class文件中的常量池,class文件中的常量池包括了字符串(数字)字面
- 学习app对excel的读写控制1.界面设计<?xml version="1.0" encoding="