1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gy.lovers;
                  
import java.util.ArrayList;
                  
import java.util.List;
                  
import java.util.Random;
                  
import android.app.Activity;
                  
import android.app.Service;
                  
import android.content.res.Resources;
                  
import android.hardware.Sensor;
                  
import android.hardware.SensorEvent;
                  
import android.hardware.SensorEventListener;
                  
import android.hardware.SensorManager;
                  
import android.os.Bundle;
                  
import android.os.Vibrator;
                  
import android.view.View;
                  
import android.widget.Button;
                  
//需要实现SensorEventListener接口
                  
public class LoversChoise extends Activity implements SensorEventListener{
                  
                         
                  
        Button clear;
                  
        //定义sensor管理器
                  
    private SensorManager mSensorManager;
                  
    //震动
                  
    private Vibrator vibrator;
                  
                     
         public void onCreate(Bundle savedInstanceState) {
                  
                super.onCreate(savedInstanceState);
                  
                setContentView(R.layout.loverschoise);
                  
                                 
              //获取传感器管理服务
                  
                mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                  
                //震动
                  
                vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                  
                                 
               clear = (Button) findViewById(R.id.clear);
                  
               clear.setOnClickListener(new Button.OnClickListener(){
                  
               @Override
                  
                public void onClick(View arg0) {
                  
                 // TODO Auto-generated method stub
                  
                   
                  
                //点击button后,给button按钮设置了text
                  
              clear.setText("现在给button的text赋值喽~");
                  
        });
                  
@Override
                  
protected void onResume(){
                  
  super.onResume();
                  
  //加速度传感器
                  
  mSensorManager.registerListener(this,
  mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  //还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等,
                  
  //根据不同应用,需要的反应速率不同,具体根据实际情况设定
                  
  SensorManager.SENSOR_DELAY_NORMAL);
                  
}
                  
@Override
                  
protected void onStop(){
                  
  mSensorManager.unregisterListener(this);
                  
  super.onStop();
                  
}
                  
@Override
                  
protected void onPause(){
                  
  mSensorManager.unregisterListener(this);
                  
  super.onPause();
                  
}
                  
@Override
                  
public void onAccuracyChanged(Sensor sensor, int accuracy) {
                  
  // TODO Auto-generated method stub
                  
  //当传感器精度改变时回调该方法,Do nothing.
                  
}
                  
@Override
                  
public void onSensorChanged(SensorEvent event) {
                  
  // TODO Auto-generated method stub
                  
  int sensorType = event.sensor.getType();
                  
                   
                  
  //values[0]:X轴,values[1]:Y轴,values[2]:Z轴
                  
  float[] values = event.values;
                  
                   
                  
  if(sensorType == Sensor.TYPE_ACCELEROMETER){
                  
                   
                  
  /*因为一般正常情况下,任意轴数值最大就在9.8~10之间,只有在你突然摇动手机
                  
  *的时候,瞬时加速度才会突然增大或减少。
                  
  *所以,经过实际测试,只需监听任一轴的加速度大于14的时候,改变你需要的设置
                  
  *就OK了~~~
                  
  */
                  
   if((Math.abs(values[0])>14||Math.abs(values[1])>14||Math.abs(values[2])>14)){
                  
                    
                  
    //摇动手机后,设置button上显示的字为空
                  
    clear.setText(null);
                  
                     
    //摇动手机后,再伴随震动提示~~
                  
    vibrator.vibrate(500);
                  
                    
                  
   }
                  
  }
                  
}
                  
}

 

本文转载自blog.csdn.net/handsome_926/article/details/7312872

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()