Search code examples
androidservicebroadcastreceiveralarmmanager

Android - Broadcast Receiver in Service, does not receive


I am using AlarmManager in order to set alarm. I have a Service, and in that Service I have a Broadcast Receiver.

The problem is, when selected time and date comes, receiver cannot receive it.

onStart part of my Service class, and where is register my receiver:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onCreate();

    IntentFilter intentFilter = new IntentFilter();
    registerReceiver(receiver, intentFilter);

    Log.i("com.example.adama", "Service has started!");

    return START_STICKY;
}

onReceive Method for receiver:

    public static BroadcastReceiver receiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        Log.i("com.example.adama", "Receiver has received!"); ...}

Part that I run service and set alarm with AlarmManager:

Intent serviceIntent = new Intent(this, BrService.class);
    startService(serviceIntent);

    Intent intent = new Intent(this, BrService.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,alarmCode++, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adama.wifiv2_spinner">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.VIBRATE"/>

<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/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".BrService"/>
</application>

What am I missing?

I am trying this since I want receive it even user closes the app.


Solution

  • If the user force-close your app, there is nothing you can do to make your receiver run, other than the user restarting your app manually.

    Other than that, please make sure that you added the BOOT_COMPLETED action to your intent filter, as well as the RECEIVE_BOOT_COMPLETED permission to your manifest, as described in this thread. This will let your receiver start up again after the phone has been rebooted (turned on and off again).

    Android BOOT_COMPLETED not received when application is closed

    Furthermore, on this thread it has been mentioned that in order for your receiver to work after the app has been closed (not force-closed), the receiver needs to be registered in the manifest:

    Is there a way to keep a repeating alarm working after existing the app that uses a broadcast receiver?

    Give it a try and let us know if it helped you :)