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:andro>27 </LinearLayout>
<uses-permissionandroid:name="android.permission.VIBRATE"/>