手机震动的节奏 Vibrator

 1 package com.turboradio.googlesdk;
2
3 import android.app.Activity;
4 import android.app.Service;
5 import android.os.Bundle;
6 import android.os.Vibrator;
7 import android.view.View;
8 import android.widget.Toast;
9 import android.widget.ToggleButton;
10
11 public class Ex5_6 extends Activity {
12 private Vibrator vibrator;
13 private ToggleButton myToggleButton1;
14 private ToggleButton myToggleButton2;
15 private ToggleButton myToggleButton3;
16 @Override
17 protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.ex_5_6);
20 vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
21 myToggleButton1 = (ToggleButton)findViewById(R.id.myToggleButton1);
22 myToggleButton2 = (ToggleButton)findViewById(R.id.myToggleButton2);
23 myToggleButton3 = (ToggleButton)findViewById(R.id.myToggleButton3);
24 }
25 /**短震动**/
26 public void toggleButton1Listener(View v){
27 if(myToggleButton1.isChecked()){
28 /**设置震动的周期**/
29 vibrator.vibrate(new long[]{100,10,100,100,1000}, -1);
30 Toast.makeText(Ex5_6.this, "短震动中..", Toast.LENGTH_LONG).show();
31 }else{
32 /**取消震动**/
33 vibrator.cancel();
34 Toast.makeText(Ex5_6.this, "震动结束!!", Toast.LENGTH_LONG).show();
35 }
36 }
37 /**长震动**/
38 public void toggleButton2Listener(View v){
39 if(myToggleButton2.isChecked()){
40 vibrator.vibrate(new long []{100,100,100,1000}, 0);
41 Toast.makeText(Ex5_6.this, "长震动...", Toast.LENGTH_LONG).show();
42 }else{
43 /**取消震动**/
44 vibrator.cancel();
45 Toast.makeText(Ex5_6.this, "长震动结束", Toast.LENGTH_LONG).show();
46 }
47 }
48 /**有节奏震动**/
49 public void toggleButton3Listener(View v){
50 if(myToggleButton3.isChecked()){
51 vibrator.vibrate(new long[]{1000,50,1000,50}, 0);
52 Toast.makeText(Ex5_6.this, "有节奏震动", Toast.LENGTH_LONG).show();
53 }else{
54 vibrator.cancel();
55 Toast.makeText(Ex5_6.this, "有节奏震动结束", Toast.LENGTH_LONG).show();
56 }
57 }
58 }
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <ToggleButton
7 android:id="@+id/myToggleButton1"
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:text="短震动"
11 android:onClick="toggleButton1Listener"
12 />
13 <ToggleButton
14 android:id="@+id/myToggleButton2"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:textOn="长震动"
18 android:onClick="toggleButton2Listener"
19 />
20 <ToggleButton
21 android:id="@+id/myToggleButton3"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="节奏震动"
25 android:onClick="toggleButton3Listener"
26 />
27 </LinearLayout>

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



转载于:https://www.cnblogs.com/jiayonghua/archive/2011/12/06/2277796.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值