Android开发中关于手机振动的问题

在Android开发中实现手机振动的功能步骤,第一步添加权限

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

第二步 配置XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="com.example.hey.alarmdemo.MainActivity">
   <Button
       android:layout_margin="10dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="shortButton"
       android:id="@+id/short_vibrate"/>
   <Button
       android:layout_margin="10dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="shortButton"
       android:id="@+id/long_vibrate"/>
   <Button
       android:layout_margin="10dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="shortButton"
       android:id="@+id/stop_vibrate"/>
</LinearLayout>

第三步 在代码中应用:

com.example.hey.alarmdemoandroid.os.Vibratorandroid.support.v7.app.AppCompatActivityandroid.os.Bundleandroid.view.Viewandroid.widget.ButtonMainActivity AppCompatActivity {
    Button Vibrator (Bundle savedInstanceState) {
        .onCreate(savedInstanceState)setContentView(R.layout.)= (Button) findViewById(R.id.)= (Button) findViewById(R.id.)= (Button) findViewById(R.id.)= (Vibrator) getSystemService().setOnClickListener(View.OnClickListener() {
            (View v) {
                .vibrate()}
        }).setOnClickListener(View.OnClickListener() {
            (View v) {
                .vibrate([]{}-)}
        }).setOnClickListener(View.OnClickListener() {
            (View v) {
                .cancel()}
        })}
}

在这里解释一下vibarator三个方法 第一个方法:

vibrator.vibrate(10000);

就是单纯的设置振动时间。

第二个方法:

vibrator.vibrate(new long[]{1000,3000,1000,3000},-1);

设置振动的节奏,new long[]{}中第一个参数表示多少毫秒开始振动,第二个参数表示振动多少毫秒,第三个参数表示停止多少毫秒,以此类推,振动时间、等待时间、振动时间、等待时间。。。-1表示只执行振动一次,0表示循环。

第三个方法:

vibrator.cancel();

表示退出振动。

注:振动的时间并不是设置多少就振动多久,我就遇到过设置振动1分钟手机却只振动30秒,在pad上运行呢!却只振动10秒。所以最终的振动时间应该跟真机本身有关,有的pad还不支持振动的功能。更注意的虚拟机不支持振动。

转载于:https://my.oschina.net/FrancisBingo/blog/662867

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值