Skip to content

Instantly share code, notes, and snippets.

@SANDY-9
Last active April 22, 2024 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SANDY-9/f31007bdd592da46d81de32feb39749c to your computer and use it in GitHub Desktop.
Save SANDY-9/f31007bdd592da46d81de32feb39749c to your computer and use it in GitHub Desktop.
기기 재부팅 상태 이벤트 수신하는 브로드캐스트리시버
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import com.sandy.seoul_matcheap.util.constants.APP_PREFS_SETTINGS
import com.sandy.seoul_matcheap.util.helper.AppPrefsUtils
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Named
@AndroidEntryPoint
class SystemBootReceiver: BroadcastReceiver() {
@Inject
lateinit var notificationScheduler: NotificationScheduler
@Inject @Named(APP_PREFS_SETTINGS)
lateinit var prefs : SharedPreferences
override fun onReceive(context: Context?, intent: Intent?) {
// BOOT_COMPLETED상태 수신
if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
// 등록된 알람이 있는지 확인하고 재예약하는 로직
val time = AppPrefsUtils.getSavedTime(prefs)
val register = AppPrefsUtils.getNotificationState(prefs)
notificationScheduler.setNotificationSchedule(register, time)
}
}
}
@SANDY-9
Copy link
Author

SANDY-9 commented Apr 22, 2024

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:name=".Application"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Seoul_Matcheap"
    android:enableOnBackInvokedCallback="true"
    android:usesCleartextTraffic="true"
    tools:targetApi="tiramisu">
    <activity
        android:name=".ui.MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
    </activity>

    <receiver
        android:name=".notification.SystemBootReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

</application>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment