A
A
Alexander Kolmachikhin2019-01-16 14:02:21
Java
Alexander Kolmachikhin, 2019-01-16 14:02:21

How to programmatically find out if the device rebooted or not?

I need my BroadcastReceiver to check whether the phone has been rebooted or not. Depending on this, the AlarmManager would be assigned.
I tested my code on android 5 and everything worked, but not on android 7.
What else needs to be added to make it work everywhere?
My check in BroadcastReceiver:

if (intent.getAction().equals(Intent.ACTION_LOCKED_BOOT_COMPLETED)
                || intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// тут я заново назначаю алармы если устройво было перезагружено
        }

My manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="kolmachikhin.alexander.todolist">

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

    <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.AppCompat.Light.DarkActionBar"
        tools:ignore="GoogleAppIndexingWarning">
        <activity.../>
        <activity.../>
        <activity.../>
        <activity.../>
<receiver android:name=".Notification.AlertReceiver"/>

        <receiver
            android:name=".Notification.RebootAlertReceiver"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question