A
A
Alexander Kolmachikhin2019-01-12 13:08:42
Java
Alexander Kolmachikhin, 2019-01-12 13:08:42

Why doesn't BroadcastReceiver work on android version 7 and above?

My app needs to send notifications at specific times. When I tested the application on my 5.2.2 android everything worked without problems. Then I tested on the smartphone version of android 7, nothing happened. What needs to be added/replaced/removed to make it work on all versions?
Here is the BroadcastReceiver code:

@Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);

        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        String title = intent.getStringExtra(NOTIFICATION_TITLE);
        String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
        int icon = intent.getIntExtra(NOTIFICATION_ICON, 0);

        NotificationCompat.Builder nb = notificationHelper.setNotification(id, title, message, icon);
        notificationHelper.getManager().notify(id, nb.build());

    }

Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<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.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">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

How do I assign an alarm:
@TargetApi(Build.VERSION_CODES.KITKAT)
    private void startAlarm() {
        contentTask = inputContent.getText().toString();

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlertReceiver.class);

        intent.putExtra(AlertReceiver.NOTIFICATION_ID, notificationId);
        intent.putExtra(AlertReceiver.NOTIFICATION_TITLE, improveSkillTitle);
        intent.putExtra(AlertReceiver.NOTIFICATION_MESSAGE, contentTask);
        intent.putExtra(AlertReceiver.NOTIFICATION_ICON, improveSkillIcon);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notificationId, intent, 0);

        assert alarmManager != null;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Varakosov, 2019-01-12
@Alexander_Kolmachikhin

Starting from 7.0 RECEIVE_BOOT_COMPLETEDis sent out after the first unlock of the device, use ACTION_LOCKED_BOOT_COMPLETED.
Also, these broadcasts are not sent if the application was installed but not launched.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question