android 之实现手机震动功能

界面:利用weight属性,能比较好得移植到平板上。

    

布局代码:

<?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.vibrator.MainActivity">

    <TextView
        android:textSize="22sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ToggleButton
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="50dp"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="@string/text2"
            android:textSize="18dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ToggleButton
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="50dp"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="@string/text1"
            android:textSize="18dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ToggleButton
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="50dp"
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="@string/text3"
            android:textSize="18dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

实现震动功能代码:
//要注意再mainfest中添加权限:<uses-permission android:name="android.permission.VIBRATE"/>
public class MainActivity extends AppCompatActivity {
    //创建震动服务对象
    private Vibrator mVibrator;
    private ToggleButton toggleButton1,toggleButton2,toggleButton3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //获取手机震动服务
        mVibrator=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        toggleButton1.setOnClickListener(new myOnClickListener1());
        toggleButton2.setOnClickListener(new myOnClickListener2());
        toggleButton3.setOnClickListener(new myOnClickListener3());
    }
    private void init(){
        toggleButton1=(ToggleButton)findViewById(R.id.button1);
        toggleButton2=(ToggleButton)findViewById(R.id.button2);
        toggleButton3=(ToggleButton)findViewById(R.id.button3);
    }
    private class myOnClickListener1 implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //判断是否开启
            if(toggleButton1.isChecked()){
                //设置震动周期,数组表示时间:等待+执行,单位是毫秒,下面操作代表:等待100,执行100,等待100,执行1000,
                //后面的数字如果为-1代表不重复,之执行一次,其他代表会重复,0代表从数组的第0个位置开始
                mVibrator.vibrate(new long[]{100,100,100,1000},-1);
                Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
            }else{
                //取消震动
                mVibrator.cancel();
                Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
            }
        }
    }
    private class myOnClickListener2 implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(toggleButton2.isChecked()){
                mVibrator.vibrate(new long[]{100,10,100,1000},0);
                Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
            }else{
                mVibrator.cancel();
                Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
            }
        }
    }
    private class myOnClickListener3 implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(toggleButton3.isChecked()){
                mVibrator.vibrate(new long[]{1000,50,1000,50,1000},0);
                Toast.makeText(MainActivity.this,getString(R.string.str_ok),Toast.LENGTH_SHORT).show();
            }else{
                mVibrator.cancel();
                Toast.makeText(MainActivity.this,getString(R.string.str_end),Toast.LENGTH_SHORT).show();
            }
        }
    }
}

原文链接:http://www.cnblogs.com/xy95/p/5870040.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值