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 ; } } }; }
复制代码

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 是一种手机提醒的方式,可以在接收到消息、来电或者其他事件时通过震动的方式提示用户。在Android中,可以使用Vibrator类来控制手机震动,需要在AndroidManifest.xml文件中添加震动权限声明。具体的实现可以参考以下代码: ``` // 获取Vibrator实例 Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // 震动500毫秒 vibrator.vibrate(500); ``` ### 回答2: Android手机震动是一种通过振动马达来产生震动效果的功能。它可以通过调用系统提供的震动API来实现。 Android手机震动功能广泛应用于各种应用程序中,例如来电提醒、消息通知、游戏反馈等。当手机处于静音或振动模式时,来电时手机会通过震动让用户知道有来电;当手机接收到消息或通知时,也会通过震动提醒用户;在游戏中,震动可以增强用户的体验和反馈,例如在打击敌人或取得游戏进步时会触发震动效果。 开发者可以通过使用Android提供的Vibrator类来控制手机震动功能。首先需要获取到Vibrator实例,然后调用其vibrate方法即可触发震动。vibrate方法通常有几种重载形式,可以指定震动持续时间、震动模式等参数。 同时,Android还提供了一些预定义的震动模式,开发者可以通过使用系统提供的常量来实现不同的震动效果,如长震动、短震动、循环震动等。此外,还可以自定义震动模式,通过指定一系列的持续时间和间隔时间来实现特殊的震动效果。 总之,Android手机震动是一项非常实用和丰富的功能,通过它可以给用户带来更好的使用体验,同时也为开发者提供了更多的交互反馈方式。 ### 回答3: Android手机震动功能是一种可以通过设置来调整的功能,用于在接收到新消息、来电或其他事件时提醒用户。 首先,用户可以在手机的设置菜单中找到“声音和振动”或类似的选项。在这个菜单中,用户可以选择是否允许手机震动。当用户将此设置打开时,手机在特定事件发生时就会震动。 通常情况下,当有新消息、来电或其他通知时,手机会发出声音和震动。但是,用户也可以根据自己的需要进行个性化设置。例如,可以设置仅在静音模式下允许震动,或者禁用声音但保留震动等。 此外,有些应用程序还提供了自定义震动模式选项。用户可以根据自己的喜好和需求,选择不同的震动模式。例如,有的人喜欢强烈的长震动,而有的人则喜欢短促的轻微震动。通过这些设置,用户可以根据自己的喜好来调整震动效果。 总的来说,Android手机震动功能是为了提醒用户新消息、来电或其他事件的一种方式。通过设置菜单,用户可以自定义是否启用震动、在哪些情况下震动以及震动的效果。这种功能的存在可以让用户在不方便或无法依赖声音提醒时,通过震动来感知到新消息或事件的到来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值