0

I'm making an app that shows a notification each time the screen is unlocked. To that purpose, I have made a BroadcastReceiver that starts a Service. Here is my manifest:

<?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="android.example.example">

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="example"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:hardwareAccelerated="true"
            android:label="example" />

        <receiver android:name=".RestartNotifications"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.USER_PRESENT"/>
            </intent-filter>
        </receiver>

        <service
            android:name=".NotificationService"
            android:enabled="true"
            android:exported="true"/>
    </application>

</manifest>

The broadcast receiver is really simple and looks like this:

public class RestartNotifications extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
        Intent i = new Intent(context, NotificationService.class);
        context.startService(i);
    }
}

I have added a Toast to check if the error is due to not receiving the broadcast or due to an error in the service. When I install the app, it works fine and the notification appears every time I unlock the phone even if another app is opened and mine is stopped. However, if I kill my app, when I unlock the phone, the notifications don't appear anymore.

Josemafuen
  • 682
  • 2
  • 16
  • 41
  • Does this answer your question? [Broadcast reciever not working when app is closed](https://stackoverflow.com/questions/44149921/broadcast-reciever-not-working-when-app-is-closed) – Codey Dec 22 '19 at 17:02

1 Answers1

0

you need to run the receiver on a Service. a simple receiver can't receive data if the app is closed check this question

Codey
  • 460
  • 1
  • 8
  • 23