Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainActivity.java
More file actions
Latest commit
55 lines (42 loc) · 1.6 KB
/
Copy pathMainActivity.java
File metadata and controls
55 lines (42 loc) · 1.6 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
packagecom.example.anany.myfirstapp;
importandroid.content.Context;
importandroid.content.pm.ActivityInfo;
importandroid.graphics.Color;
importandroid.graphics.Typeface;
importandroid.os.Vibrator;
importandroid.support.v4.content.ContextCompat;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassMainActivityextendsAppCompatActivity {
Buttonbtn;
Vibratorvib;
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//delay, play, delay, play, delay (milliseconds)
long[] pattern = {0,300,500,100,600};
//repeat the pattern from the 0th index in the array.
vib.vibrate(pattern, 0);
//If the device can vibrate, display a text saying it can vibrate otherwise say the opposite.
if(vib.hasVibrator())
makeToast("Can Vibrate!");
else
makeToast("Device can't vibrate.");
//When the button is pressed, stop the vibrations.
btn.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(Viewv) {
vib.cancel();
}
});
}
privatevoidmakeToast(Strings) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}