Android(Kotlin)

Android BOOT_COMPLETED

----___<<<<< 2022. 10. 20. 02:36

 

<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 - ACTION_BOOT_COMPLETED 이벤트 받기

안드로이드는 부팅이 완료되면 ACTION_BOOT_COMPLETED 인텐트를 전달하여 앱이 실행되도록 합니다. 앱은 이 인텐트를 받고, 어떤 작업을 처리할 수 있습니다. 또한, BOOT_COMPLETED 인텐트를 받고 내 앱의

codechacha.com

 

'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