-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
145 lines (93 loc) · 3.69 KB
/
MainActivity.java
File metadata and controls
145 lines (93 loc) · 3.69 KB
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
/*Hi
This code is written in ANDROID STUDIO
JAVA CODE
*/
package com.example.sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private ImageView imageview;
private ImageView imageview2;
private SensorManager sensormanager;
private Sensor accelerometerSensor;
private boolean isavailable;
private float currentXcordinate , currentYcordinate, currentZcordinate, lastXcordinate, lastYcordinate, lastZcordinate, xDifference, yDifference, zDifference,threshold=5f;
private boolean isnotfirst=false;
private Vibrator vibrator;
private boolean initiate=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = findViewById(R.id.imageView8);
imageview2 = findViewById(R.id.imageView5);
vibrator= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
sensormanager =(SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)!=null)
{
accelerometerSensor=sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
isavailable=true;
}else
{
isavailable=false;
}
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
currentXcordinate= sensorEvent.values[0];
currentYcordinate= sensorEvent.values[1];
currentZcordinate= sensorEvent.values[2];
if(isnotfirst)
{
xDifference=Math.abs(lastXcordinate-currentXcordinate);
yDifference=Math.abs(lastYcordinate-currentYcordinate);
zDifference=Math.abs(lastZcordinate-currentZcordinate);
if((xDifference>threshold && yDifference>threshold)||
(yDifference>threshold && zDifference>threshold)||
(xDifference>threshold && zDifference>threshold)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(500);
}
if (initiate) {
imageview.setImageResource(R.drawable.maroon2);
imageview2.setImageResource(R.drawable.sky);
initiate = false;
} else {
imageview.setImageResource(R.drawable.sky);
imageview2.setImageResource(R.drawable.maroon2);
initiate = true;
}
}
}
lastXcordinate=currentXcordinate;
lastYcordinate=currentYcordinate;
lastZcordinate=currentZcordinate;
isnotfirst=true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
protected void onResume(){
super.onResume();
if(isavailable)
sensormanager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause(){
super.onPause();
if(isavailable)
sensormanager.unregisterListener(this);
}
}