-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
sensor_light.txt
59 lines (44 loc) · 1.41 KB
/
sensor_light.txt
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
// permission
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
//code
public class MainActivity extends AppCompatActivity implements SensorEventListener {
Sensor sensor;
SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public void onResume(){
super.onResume();
sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onPause(){
super.onPause();
sensorManager.unregisterListener(this);
}
MediaPlayer mp;
boolean isrunning=false;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0]>40 && isrunning==false){
isrunning=true;
mp=new MediaPlayer();
try {
mp.setDataSource("http://server6.mp3quran.net/thubti/001.mp3");
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}