<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
class BootReceiver : BroadcastReceiver() {
private val TAG = BootReceiver::class.java.simpleName
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "onReceive")
val randomUuid = UUID.randomUUID()
val cFormat = SimpleDateFormat("hh:mm:ss")
val currentDate = cFormat.format(Date())
val file:String = currentDate
val data:String = intent?.action.toString()
val fileOutputStream: FileOutputStream
try {
if (context != null) {
fileOutputStream = context.openFileOutput("$file $randomUuid", Context.MODE_PRIVATE)
fileOutputStream.write(data.toByteArray())
}
} catch (e: FileNotFoundException){
e.printStackTrace()
}catch (e: NumberFormatException){
e.printStackTrace()
}catch (e: IOException){
e.printStackTrace()
}catch (e: Exception){
e.printStackTrace()
}
}
}
- 참조
https://codechacha.com/ko/android-action-boot-completed/
'Android(Kotlin)' 카테고리의 다른 글
Android Shimmer (0) | 2022.10.22 |
---|---|
Android ViewModel Context (0) | 2022.10.20 |
Simple ForegroundService (0) | 2022.10.15 |
Android ROOM sqlcipher (0) | 2022.09.20 |
Android AlarmManager (0) | 2022.09.16 |