栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现手机振动设置的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现手机振动设置的方法

本文实例讲述了Android实现手机振动设置的方法。分享给大家供大家参考。具体如下:

main.xml布局文件:



  
    
    
  
  
    
    
  

清单文件:



  
    
      
 
 
      
    
  
  
  
  

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号