user754730
user754730

Reputation: 1340

Broadcastreceiver not receiving triggered Alarm

I have the following problem. I have 2 classes. 1 is called AlarmService and the other is called TimeAlarm which extends BroadcastReceiver. The App should do the following thing: It should generate a new Alarm to a time specified in the preferences (Which it already does...) also in Logcat i can see how the Alarm gets triggered. But the problem is, that the Notification which should be shown does not show up in the StatusBar.

Here is all the code i have for this:

AndroidManifest.xml:

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

AlarmService.java:

private Context context;
private PendingIntent mAlarmSender;

public AlarmService(Context context) {
    this.context = context;
    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(context, myActivity.class);
    mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}

public void startAlarm(int stunde, int minute) {
    Calendar updateTime = Calendar.getInstance();

    updateTime.set(Calendar.HOUR_OF_DAY, stunde);
    updateTime.set(Calendar.MINUTE, minute);
    updateTime.set(Calendar.SECOND, 00);

    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, mAlarmSender);
}

TimeAlarm.java:

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("TEST", "onReceive() called...");
}

The Receiver in the Manifest is inside of the Tag so that should not be a problem. The problem is, that if i restart my device, it gets called. But not if an Alarm gets triggered. But the onReceive() method should also be called if an alarm gets triggered, shouldn't it?

Thanks for your help!

Upvotes: 1

Views: 3007

Answers (1)

senola
senola

Reputation: 782

your intent filter is only listening to boot complete intents and not your own alarm broadcast intent action. update your intent filter so that your broadcast intent is also received (that means for your special case add the action of Intent.ACTION_MAIN to your intent filter)

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</receiver>

Upvotes: 2

Related Questions