package com.red_folder.phonegap.plugin.scheduler;

import com.red_folder.phonegap.helpers.AppHelper;
import com.red_folder.phonegap.plugin.scheduler.dal.SchedulerDAL;
import com.red_folder.phonegap.plugin.scheduler.models.Alarm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;


public class AlarmBroadcastReceiver extends BroadcastReceiver {

	private final static String TAG = AlarmBroadcastReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(TAG, "Starting onReceive of broadcast");

		Bundle extras = intent.getExtras();

		if (extras == null || !extras.containsKey("id")) {
			Log.d(TAG, "Extras is null or doesn't inlcude an ID, thus ignoring broadcast");
		} else {
			SchedulerDAL dal = new SchedulerDAL(context);

			int id = extras.getInt("id");
			Alarm alarm = dal.get(id);
			

			if (alarm == null) {
				Log.d(TAG, "No alarm found for given ID, thus ignoring broadcast");
			} else {
				
				dal.delete(alarm);
				dal.save();
				
				PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
				PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
				//Acquire the lock
				wl.acquire();

				Log.d(TAG, "Asked to Start app - " + alarm.getClassName());

				if (alarm.getClassName() != null) {
					if (AppHelper.isServiceRunning(context, alarm.getClassName())) {
						Log.d(TAG, alarm.getClassName() + " already running");
					} else {
						Log.d(TAG, alarm.getClassName() + " starting");
						AppHelper.startActivity(context, alarm.getClassName());
					}
				}
				
				//Release the lock
				wl.release();
			}
			
			dal = null;

		}

	}
}

@@ -1457,24 +1457,235 @@
 ull) %7B%0A%09%09%09%09%09
+//commented out to ensure it's brought to foreground even if active in background%0A%09%09%09%09%09// note: it then requires in Android-manifest: android:launchMode=%22singleTask%22 or android:launchMode=%22singleIstance%22%0A%09%09%09%09%09//
 if (AppHelpe
@@ -1734,32 +1734,34 @@
 Name())) %7B%0A%09%09%09%09%09
+//
 %09Log.d(TAG, alar
@@ -1805,16 +1805,18 @@
 );%0A%09%09%09%09%09
+//
 %7D else %7B
@@ -1932,21 +1932,23 @@
 ame());%0A
-
 %09%09%09%09%09
+//
 %7D%0A%09%09%09%09%7D%0A

Update AlarmBroadcastReceiver.java