이번 시간에는 진동으로 알림 효과 내는 방법에 대하여 알아보겠습니다.
- 설명 -
진동 효과를 내기 위해서는 권한을 등록해야 합니다.
application 태그 위에 입력하면 됩니다.
manifests -> AndroidManifest.xml
<uses-permission android:name="android.permission.VIBRATE" />
- 설명 -
1. 진동 기능 Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/vibBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="진동"
android:textSize="20sp" />
</LinearLayout>
- 설명 -
1. getSystemService(VIBRATOR_SERVICE)
진동 객체 가져오기
2. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
안드로이드 버전 8 기준으로 진동 기능 구현이 달라집니다.
3. VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)
진동 시간과 진동 세기를 조절할 수 있습니다. 진동 세기는 1 ~ 255 정할 수도 있습니다.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val vibBtn: Button = findViewById(R.id.vibBtn)
vibBtn.setOnClickListener {
//진동
showVibrator()
}
}
//진동 이벤트
private fun showVibrator(){
val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
//버전 오레오, 그 이상(안드로이드 8)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//진동시간, 세기 설정(0.5초, 기본세기)
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
}else{ //오레오 이하
//0.5초동안 울림
vibrator.vibrate(500)
}
}
}
2022.09.26 - [안드로이드] - [안드로이드 코틀린] Ringtone 효과음 내는 방법
2022.09.21 - [안드로이드] - [안드로이드 코트린] SharedPreferences 간단한 데이터 저장하는 방법
2022.09.20 - [안드로이드] - [안드로이드 코틀린] 코드(MainActivity)에서 배경 색상 바꾸는 다양한 방법
[안드로이드 코틀린] HTTP 통신 Retrofit2 사용법 part2 - 화면 및 기능 (0) | 2022.09.29 |
---|---|
[안드로이드 코틀린] HTTP 통신 Retrofit2 사용법 part1 - 권한 및 설정 (0) | 2022.09.28 |
[안드로이드 코틀린] Ringtone 효과음 내는 방법 (0) | 2022.09.26 |
[안드로이드 코틀린] RecyclerView 다중 선택 삭제하는 방법 (0) | 2022.09.23 |
[안드로이드 코틀린] RecyclerView 다중 선택 색상 변경하는 방법 (2) | 2022.09.22 |
댓글 영역