Alexander_Kolmachikhin
@Alexander_Kolmachikhin
Android Programming

Почему не работает BroadcastReceiver на андроидах версии 7 и выше?

Мое приложение должно отправлять уведомления в определенное время. Когда я тестировал приложение на своем 5.2.2 андроиде все работало без проблем. Затем протестил на смартфоне версии андроида 7, ничего не происходило. Что нужно добавить/заменить/убрать чтобы работало на всех версиях?

Вот код BroadcastReceiver'a:
@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());

    }


Код манифеста:
<?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>


Как я назначаю будильник:
@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);
    }
  • Вопрос задан
  • 537 просмотров
Решения вопроса 1
thelongrunsmoke
@thelongrunsmoke
Программист
Начиная с 7.0 RECEIVE_BOOT_COMPLETED рассылается после первой разблокировки устройства, используйте ACTION_LOCKED_BOOT_COMPLETED.
Кроме того, эти броадкасты не присылаются, если приложение было установлено, но не запускалось.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы