<uses-permission android:name="android.permission.VIBRATE"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c52">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt
package com.tutorial.c52
import android.content.Context
import android.media.RingtoneManager
import android.net.Uri
import android.os.*
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val vibButton = findViewById<Button>(R.id.vibration)
val beepButton = findViewById<Button>(R.id.beep)
vibButton.setOnClickListener {
val vibrator = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){ // api level 31 이상
val vibManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibManager.defaultVibrator
}else{
getSystemService(VIBRATOR_SERVICE) as Vibrator
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ // api level 26 이상
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
}else {
vibrator.vibrate(500)
}
}
beepButton.setOnClickListener {
val uri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val ringtone = RingtoneManager.getRingtone(applicationContext, uri)
ringtone.play()
}
}
}
https://developer.android.com/reference/android/app/AlertDialog
package com.tutorial.c53
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Button
import android.widget.DatePicker
import android.widget.TimePicker
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val alertDialog = findViewById<Button>(R.id.alertDialog)
val listDialog = findViewById<Button>(R.id.listDialog)
val dateDialog = findViewById<Button>(R.id.dateDialog)
val timeDialog = findViewById<Button>(R.id.timeDialog)
alertDialog.setOnClickListener {
AlertDialog.Builder(this).run {
setTitle("test dialog")
setIcon(android.R.drawable.ic_dialog_alert)
setMessage("정말 종료하시겠습니까?")
setPositiveButton("OK", null)
setNegativeButton("Cancel", null)
show()
}
}
listDialog.setOnClickListener {
val items = arrayOf("사과", "복숭아", "수박")
AlertDialog.Builder(this).run {
setTitle("items test")
setItems(items, object: DialogInterface.OnClickListener{
override fun onClick(p0: DialogInterface?, pos: Int) {
Toast.makeText(this@MainActivity,
"선택한 항목: ${items[pos]}", Toast.LENGTH_SHORT).show()
}
})
setPositiveButton("OK", null)
show()
}
}
dateDialog.setOnClickListener {
DatePickerDialog(this, object: DatePickerDialog.OnDateSetListener{
override fun onDateSet(p0: DatePicker?, year: Int, month: Int, day: Int) {
Toast.makeText(this@MainActivity,
"${year}, ${month + 1}, ${day}", Toast.LENGTH_SHORT).show()
}
}, 2022, 10, 12).show()
}
timeDialog.setOnClickListener {
TimePickerDialog(this, object: TimePickerDialog.OnTimeSetListener{
override fun onTimeSet(p0: TimePicker?, p1: Int, p2: Int) {
Toast.makeText(this@MainActivity,
"$p1, $p2", Toast.LENGTH_SHORT).show()
}
}, 0, 0, true).show()
}
}
}