Add a vibration and a ringtone notification 진동, 링톤 알림 추가하기
Android 2019. 11. 24. 10:46 |반응형
Add a vibration and a ringtone notification to your android application.
<AndroidManifest.xml>
1 | <uses-permission android:name="android.permission.VIBRATE"/> |
<MainActivity.java>
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 | public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); if (Build.VERSION.SDK_INT >= 26) { vibrator.vibrate(VibrationEffect.createOneShot(1000, 10)); // - public static VibrationEffect createOneShot (long milliseconds, int amplitude) // Create a one shot vibration. One shot vibrations will vibrate constantly for the specified period of time at the specified amplitude, and then stop. // milliseconds long: The number of milliseconds to vibrate. This must be a positive number. // amplitude int: The strength of the vibration. This must be a value between 1 and 255, or DEFAULT_AMPLITUDE. } else { vibrator.vibrate(1000); // Vibrate constantly for the specified period of time. // This method was deprecated in API level 26. Use vibrate(android.os.VibrationEffect) instead. } } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // - public static Uri getDefaultUri (int type) // Returns the Uri for the default ringtone of a particular type. Rather than returning the actual ringtone's sound Uri, this will return the symbolic Uri // which will resolved to the actual sound when played. // - public static final int TYPE_NOTIFICATION // Type that refers to sounds that are used for notifications. // Constant Value: 2 (0x00000002) Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri); // Returns a Ringtone for a given sound URI. // If the given URI cannot be opened for any reason, this method will attempt to fallback on another sound. If it cannot find any, it will return null. ringtone.play(); // Plays the ringtone. } }); Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.media); // Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again. // When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception. mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); // Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. } }); // Interface definition for a callback to be invoked when playback of a media source has completed. mediaPlayer.start(); // Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never // started before, playback will start at the beginning. } }); } } |
Add a media file for MediaPlayer.
Run the app and click the buttons.
반응형
'Android' 카테고리의 다른 글
Multiple buttons with one listener 한 개의 리스너로 버튼 여러개 처리하기 (0) | 2019.12.04 |
---|---|
How to issue a notification 간단한 알림 표시 하기 (0) | 2019.11.26 |
WebView - Display web content as part of your activity layout 앱 안에서 인터넷 웹 페이지 디스플레이 하기 (0) | 2019.11.11 |
Play Youtube videos with YouTubePlayer 유튜브 플레이어로 유튜브 영상 재생하기 (0) | 2019.10.24 |
Stream a video file with VideoView 비디오 파일 스트리밍 하기 (0) | 2019.10.22 |