Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintentandroid-broadcastreceiver

BroadcastReceiver not being called by Alarm(Manager)


Can you spot any reason as to why BroadcastReceiver is not being called when the Alarm goes off? If I have the alarm launch an explicit intent, it works just fine and my activity opens. If I set the intent to open my BroadCastReceiver then nothing happens so I think there may be something wrong with my receiver class or the Manifest. Here's how I setup the alarm:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 324, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Here's my broadcast receiver:

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MJH", "Alarm called...");
        Toast.makeText(context, "Alarm...", Toast.LENGTH_LONG).show();
    }
}

And here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="mjh.com.apod"
      xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Required to act as a custom watch face. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>
    <receiver
        android:name=".AlarmBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        android:process=":remote">
    </receiver>
</application>

Thank you so very much for your time.


Solution

  • I think you should be using PendingIntent.getBroadcast( instead of PendingIntent.getActivity(.