Alarm Manager doesn't work in application with BroadcastReceiver

505 views Asked by At

I'm doing an application, that uses geofences to track our events and I want to create an alarm manager, which is activated daily and adds geofences for current day events (taken from database).

I needed to create an activity to use geofences and in this activity, I put BroadcastReceiver. It seems that it's not working at all. I don't even get any logs from these activities. I don't know why, but I suppose something can be wrong in Manifest. I would be grateful if someone would look at this.

Class MyScheduler:

public class MyScheduler extends Activity {


protected static final String TAG = "MyScheduler";

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setRecurringAlarm(this);
}

private void setRecurringAlarm(Context context) {

    Log.i(TAG, "setReccuringAlarm");
    Calendar updateTime = Calendar.getInstance();
    updateTime.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
    updateTime.set(Calendar.HOUR_OF_DAY, 15);
    updateTime.set(Calendar.MINUTE, 10);

    Intent intent = new Intent(context, Tasks.class);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) this.getSystemService(
            Context.ALARM_SERVICE);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, recurringDownload);
    }
}

Tasks with BroadcastReceiver:

public class Tasks  extends Activity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
Context context;
final public static String ONE_TIME = "onetime";

EventDAO eventDAO;

protected static final String TAG = "MyGeofence";

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String today = dateFormat.format(calendar.getTime());
List<Event> eventsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = getApplicationContext();

    mGeofenceList = new ArrayList<Geofence>();
    mGeofencePendingIntent = null;
    mSharedPreferences = getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
            MODE_PRIVATE);
    mGeofencesAdded = mSharedPreferences.getBoolean(Constants.GEOFENCES_ADDED_KEY, false);

    buildGoogleApiClient();
}

/// geofences mathods, not needed

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override

    public void onReceive(Context context, Intent intent) {
        Log.i(ONE_TIME, "Executed Tasks.Java File");
        //Some task here for every morning
        eventDAO = new EventDAO(context);
        eventsList = eventDAO.getComingEvents(today, today);

        for( Event event : eventsList){
            String place = event.getEndLocalisationX();
            Log.i(ONE_TIME, place);

            Double lat = Double.parseDouble(event.getEndLocalisationY());
            Double lng = Double.parseDouble(event.getEndLocalisationX());
            MY_PLACES.put(place, new LatLng(lat, lng));
        }

        populateGeofenceList();
        addGeofences();
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver);
}

And part of the manifest:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <activity android:name=".MyScheduler"/>
    <activity android:name=".Tasks"/>

    <receiver android:name=".Tasks" >
     </receiver>

I can add, that everything with geofences works well, so I didn't put the geofences code above, as it's easier to read. Hope someone will be able to help! :)

1

There are 1 answers

3
Johnny C On

You're creating a PendingIntent to send a broadcast, but you specified an Activity class (Tasks). You shouldn't set an alarm to interrupt the user with a UI, if Android even allows that. You should move your Broadcast receiver in to an outer class such as TasksReceiver:

public class TasksReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
    Log.i(ONE_TIME, "Executed Tasks.Java File");
    //Some task here for every morning
    EventDAO eventDAO = new EventDAO(context);
    List<Event> eventsList = eventDAO.getComingEvents(today, today);

    for( Event event : eventsList){
        String place = event.getEndLocalisationX();
        Log.i(ONE_TIME, place);

        Double lat = Double.parseDouble(event.getEndLocalisationY());
        Double lng = Double.parseDouble(event.getEndLocalisationX());
        MY_PLACES.put(place, new LatLng(lat, lng));
    }

    populateGeofenceList();
    addGeofences();
   }
}

and specify that class name for the pending intent and the receiver declaration in the manifest would look like:

<receiver android:name=".TasksReceiver" />