[안드로이드 스튜디오/코틀린] 진동, 푸시 알림
Vibration
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="BUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.View
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btn.setOnClickListener { Vibration(1000L) }
}
fun Vibration(len:Long) {
val vibrator = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as Vibrator
vibrator.vibrate(len)
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shinyelee.android_study2">
<!-- 이 위치에 코드 추가 -->
<uses-permission android:name="android.permission.VIBRATE"/>
<!-- 나머지 생략 -->
</manifest>
Push Notification
activity_main.xml
<!-- Vibration과 동일 -->
MainActivity.kt
package com.shinyelee.android_study2
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btn.setOnClickListener { ShowNotification("제목", "내용") }
}
fun ShowNotification(Title: String, Body: String) {
val pending = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_CANCEL_CURRENT)
val builder = NotificationCompat.Builder(this, "id")
builder.setSmallIcon(R.drawable.ic_baseline_create_24)
.setContentTitle(Title)
.setContentText(Body)
.setContentIntent(pending)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE)
val NManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NManager.createNotificationChannel(NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH))
}
NManager.notify(0, builder.build())
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Time Picker, Saving Data (0) | 2022.05.17 |
---|---|
[Android Studio/Kotlin] Dialog, Share (0) | 2022.05.17 |
[Android Studio/Kotlin] Tool Bar (0) | 2022.05.12 |
[Android Studio/Kotlin] Guide Line (0) | 2022.05.11 |
[Android Studio/Kotlin] Search View, Spinner (0) | 2022.05.10 |