Android Vibrator 震动 消息通知

  过年比较忙,家里也没网,懒惰了好几天,终于又要开始了。每天都有事,所以就写点简单的。我们平常在用QQ或者微信的时候,经常会把手机调成震动模式,这个时候如果有新消息的话,就会听到手机震动,但是我们也会发现,每种应用程序的震动的时间和周期不一样,那么我们就来看看怎么样来控制手机的震动。

  手机震动属于硬件控制,所以要import两个包,一个是提供用于震动的服务android.app.Service,一个是用来管理震动的包android.os.Vibrator。重点就在这个Vibrator。那么我们打开帮助文档。大致的解释一下:Vibrator这个类是用来管理手机硬件的震动器。如果你的程序退出,那么你在这个程序中使用的所有振动都将停止。通过getSystemService(VIBRATOR_SERVICE)来从系统服务中获取一个Vibrator实例。

  看完了大致介绍,接着来看看他的函数,这个类其实非常简单,常用的方法只有四个。所以就一一介绍吧。

  public abstract void cancel ():关闭振动器。

  public abstract boolean hasVibrator ():判断硬件设备是否有振动器。有的话返回true,否则false

  public abstract void vibrate (long[] pattern, int repeat):这个是用的最多的方法,看名字就知道是振动,那么重要的就是他的两个参数了。第一个参数是一个long型的数组。因为振动器的振动单位都是milliseconds 毫秒级的,所以一定要注意数值的大小。数组中的第一个值用来指定设备开始振动前的毫秒数,接下来的第二个数值用来指定振动器关闭前的震动时间,剩下的数值序列用来循环震动。即系统将按照给定的序列周期去启动和关闭振动器。函数的第二个参数repeat是重复的次数,如果值为-1,则只震动一次,如果值为0,则一直按照数组的序列值去震动,如果大于0,则在这个数组指定的下标初开始循环,即这个参数既可以控制循环的次数,也可以指定从哪儿开始震动周期。

  public abstract void vibrate (long milliseconds):这个函数参数一看就明白这是震动的时间。

  看了文档之后接下来就是来实践了。这里采用ToggleButton来控制震动的开和关。首先是布局文件

  

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:layout_width="fill_parent"

 4     android:layout_height="fill_parent"

 5     android:orientation="vertical"

 6     tools:context=".MainActivity" >

 7 

 8     <LinearLayout

 9         android:layout_width="fill_parent"

10         android:layout_height="wrap_content"

11         android:orientation="horizontal" >

12 

13         <ToggleButton

14             android:id="@+id/togglebutton1"

15             android:layout_width="wrap_content"

16             android:layout_height="wrap_content"

17             android:textOff="off"

18             android:textOn="on" />

19 

20         <TextView

21             android:layout_width="wrap_content"

22             android:layout_height="wrap_content"

23             android:text="短震动"

24             android:textSize="16sp" />

25     </LinearLayout>

26 

27     <LinearLayout

28         android:layout_width="fill_parent"

29         android:layout_height="wrap_content"

30         android:orientation="horizontal" >

31 

32         <ToggleButton

33             android:id="@+id/togglebutton2"

34             android:layout_width="wrap_content"

35             android:layout_height="wrap_content"

36             android:textOff="off"

37             android:textOn="on" />

38 

39         <TextView

40             android:layout_width="wrap_content"

41             android:layout_height="wrap_content"

42             android:text="长震动"

43             android:textSize="16sp" />

44     </LinearLayout>

45 

46     <LinearLayout

47         android:layout_width="fill_parent"

48         android:layout_height="wrap_content"

49         android:orientation="horizontal" >

50 

51         <ToggleButton

52             android:id="@+id/togglebutton3"

53             android:layout_width="wrap_content"

54             android:layout_height="wrap_content"

55             android:textOff="off"

56             android:textOn="on" />

57 

58         <TextView

59             android:layout_width="wrap_content"

60             android:layout_height="wrap_content"

61             android:text="节奏震动"

62             android:textSize="16sp" />

63     </LinearLayout>

64 

65 </LinearLayout>

 

  接下来是功能代码,在理解了函数之后就比较简单了。

  

 1 package com.example.ex0506;

 2 

 3 import android.os.Bundle;

 4 import android.os.Vibrator;

 5 import android.app.Activity;

 6 import android.app.Service;

 7 import android.view.Menu;

 8 import android.view.View;

 9 import android.view.View.OnClickListener;

10 import android.widget.ToggleButton;

11 

12 public class MainActivity extends Activity {

13     private Vibrator mVibrator;

14     ToggleButton mToggleButton1, mToggleButton2, mToggleButton3;

15 

16     @Override

17     protected void onCreate(Bundle savedInstanceState) {

18         super.onCreate(savedInstanceState);

19         setContentView(R.layout.activity_main);

20         mVibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);

21         mToggleButton1 = (ToggleButton) this.findViewById(R.id.togglebutton1);

22         mToggleButton2 = (ToggleButton) this.findViewById(R.id.togglebutton2);

23         mToggleButton3 = (ToggleButton) this.findViewById(R.id.togglebutton3);

24         mToggleButton1.setOnClickListener(new OnClickListener() {

25 

26             @Override

27             public void onClick(View v) {

28                 // TODO Auto-generated method stub

29                 if (mToggleButton1.isChecked()) {

30                     mVibrator.vibrate(new long[]{100,10,100,1000},-1);

31                 } else {

32                     mVibrator.cancel();

33                 }

34             }

35         });

36         mToggleButton2.setOnClickListener(new OnClickListener() {

37 

38             @Override

39             public void onClick(View v) {

40                 // TODO Auto-generated method stub

41                 if (mToggleButton2.isChecked()) {

42                     mVibrator.vibrate(new long[]{100,100,100,1000}, 2);

43                 } else {

44                     mVibrator.cancel();

45                 }

46             }

47         });

48         mToggleButton3.setOnClickListener(new OnClickListener() {

49 

50             @Override

51             public void onClick(View v) {

52                 // TODO Auto-generated method stub

53                 if (mToggleButton3.isChecked()) {

54                     mVibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);

55                 } else {

56                     mVibrator.cancel();

57                 }

58             }

59         });

60     }

61 

62     @Override

63     public boolean onCreateOptionsMenu(Menu menu) {

64         // Inflate the menu; this adds items to the action bar if it is present.

65         getMenuInflater().inflate(R.menu.main, menu);

66         return true;

67     }

68 

69 }

  当然,因为是系统服务所以一定记住要在manifest中添加权限。

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

 

你可能感兴趣的:(android)