zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
    }
  • 相关阅读:
    Hibernate 组合主键映射
    Hibernate 对象的生命周期及CRUD操作
    Hibernate *.hbm.xml对象关系映射文件详解
    Hibernate.cfg.xml详解
    hibernate4日志配置
    Hibernate第一个程序
    hibernate-release-4.3.11.Final资源包介绍
    (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
    redis CONFIG REWRITE介绍
    (转)Linux core 文件介绍与处理
  • 原文地址:https://www.cnblogs.com/xy95/p/5870040.html
Copyright © 2011-2022 走看看