Android Open Source - MapAlarmist Receive Transitions Intent Service






From Project

Back to project page MapAlarmist.

License

The source code is released under:

Copyright 2013 Guilhem Vellut <guilhem@vellut.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), t...

If you think the Android project MapAlarmist listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vellut.geoalarm;
// w w w . ja va 2  s . c  o  m
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;

public class ReceiveTransitionsIntentService extends IntentService {

  public ReceiveTransitionsIntentService() {
    super("ReceiveTransitionsIntentService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Log.d(GeoAlarmUtils.APPTAG,
        "ReceiveTransitionsItentService HandleIntent");

    // First check for errors
    if (LocationClient.hasError(intent)) {
      int errorCode = LocationClient.getErrorCode(intent);
      // Log the error
      Log.e(GeoAlarmUtils.APPTAG,
          getString(R.string.geofence_transition_error_detail,
              errorCode));
    } else {
      int transition = LocationClient.getGeofenceTransition(intent);
      // Test that a valid transition was reported
      if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {
        Log.d(GeoAlarmUtils.APPTAG, "GeoAlarm triggered");
        long alarmSetTime = intent.getExtras().getLong(
            GeoAlarmUtils.EXTRA_ALARM_SET_TIME);
        long currentTime = SystemClock.elapsedRealtime();
        if (currentTime - alarmSetTime > GeoAlarmUtils.MIN_DTIME) {
          boolean isUseVbirate = intent.getExtras().getBoolean(
              GeoAlarmUtils.EXTRA_USE_VIBRATE);
          String ringtoneUri = intent.getExtras().getString(
              GeoAlarmUtils.EXTRA_RINGTONE_URI);
          sendNotification(ringtoneUri, isUseVbirate);
        }
      }
    }

    ReceiveTransitionsBroadcastReceiver.completeWakefulIntent(intent);
  }

  private void sendNotification(String ringtoneUri, boolean isUseVibrate) {

    // Create an explicit content Intent that starts the main Activity
    Intent notificationIntent = new Intent(getApplicationContext(),
        MainActivity.class);
    notificationIntent.setAction(GeoAlarmUtils.ACTION_STOP_ALARM);

    // Construct a task stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Adds the main Activity to the task stack as the parent
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack
    PendingIntent notificationPendingIntent = stackBuilder
        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions
    // >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
        this);

    // Set the notification contents
    builder.setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(
            getString(R.string.geofence_transition_notification_title))
        .setContentText(
            getString(R.string.geofence_transition_notification_text))
        .setContentIntent(notificationPendingIntent)
        .setAutoCancel(true);

    if (ringtoneUri != null) {
      builder.setSound(Uri.parse(ringtoneUri),
          Notification.STREAM_DEFAULT);
    }

    if (isUseVibrate) {
      builder.setVibrate(new long[] { 0, 200, 1000, 200, 1000, 200, 1000,
          200, 1000 });
    }

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(GeoAlarmUtils.GEOFENCE_NOTIFICATION_ID,
        builder.build());
  }

}




Java Source Code List

com.vellut.geoalarm.AlarmWakeUpBroadcastReceiver.java
com.vellut.geoalarm.AlarmWakeUpService.java
com.vellut.geoalarm.GeoAlarmBootServiceStarter.java
com.vellut.geoalarm.GeoAlarmBootService.java
com.vellut.geoalarm.GeoAlarmUtils.java
com.vellut.geoalarm.GeoAlarm.java
com.vellut.geoalarm.MainActivity.java
com.vellut.geoalarm.ReceiveTransitionsBroadcastReceiver.java
com.vellut.geoalarm.ReceiveTransitionsIntentService.java
com.vellut.geoalarm.SavedLocation.java
com.vellut.geoalarm.SettingsActivity.java
com.vellut.geoalarm.SwipeDismissListViewTouchListener.java
com.vellut.geoalarm.io.LatLngBoundsDeserializer.java
com.vellut.geoalarm.io.LatLngBoundsSerializer.java