How can I pass data from an Activity to a Geofence BroadcastReceiver? - java

I was building a Geofencing Application to track the number of people in the given region at any given time and storing their IDs on a Backend.
I am unable to figure out how to pass the ID I take from the user and to the GeofenceBroadcastReceiver where I am incrementing and decrementing the number of people in the onReceive so I can keep a track of who all are in there.
I don't think it can be passed through an intent as I am not the one calling the Receiver and I am unfamiliar of the inner workings of the process.
MainActivity.java
public void addGeofence(LatLng latLng, float radius) {
Geofence geofence = geofenceHelper.getGeofence(GEOFENCE_ID, latLng, radius, Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT);
GeofencingRequest geofencingRequest = geofenceHelper.getGeofencingRequest(geofence);
PendingIntent pendingIntent = geofenceHelper.getPendingIntent();
geofencingClient.addGeofences(geofencingRequest, pendingIntent).addOnSuccessListener(
\\code );
GeofenceHelper.java
public class GeofenceHelper extends ContextWrapper {
PendingIntent pendingIntent;
public GeofenceHelper(Context base) {
super(base);
}
public GeofencingRequest getGeofencingRequest(Geofence geofence) {
return new GeofencingRequest.Builder()
.addGeofence(geofence)
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.build();
}
public Geofence getGeofence(String ID, LatLng latLng, float radius, int transitionTypes) {
return new Geofence.Builder()
.setCircularRegion(latLng.latitude, latLng.longitude, radius)
.setRequestId(ID)
.setTransitionTypes(transitionTypes)
.setLoiteringDelay(5000)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
}
public PendingIntent getPendingIntent() {
if (pendingIntent != null) {
return pendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}

No need to pass data from activity to GeofenceBroadcastReceiver. When user enters geofence area, GeofenceBroadcastReceiver onReceive() automatically will be called by android system if you properly registered geofence.
First register your geofence once added successfully, in your emulator set your registered geofence location and move around to trigger enter and exit of geofence. When triggered, onReceive() automatically will be called and you can get triggered id from onReceive() intent. Then you can increment and decrement number of people with in onReceive()

Related

Show alert instead of scheduled notification if the app is currently opened

I have an app that schedules a bunch of notifications (user has to answer questionnaires) locally using AlarmManager. The notification should show at certain points in the future.
I schedule the notifications like this:
private void scheduleNotification(Notification notification, int delay, int scheduleId, int notificationId) {
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(NotificationPublisher.INTENT, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, scheduleId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, delay);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
The intent is received by a BroadcastReceiver that calls notify on the notification attached to the intent.
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String INTENT = "notification";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(INTENT)) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Notification notification = intent.getParcelableExtra(INTENT);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
}
This works fine so far. The problem that I'm facing is that I only want to show the notification if the app is currently not open/shown. If it's open I want to show an AlertDialog instead.
I know that it might be a better idea to put only the plain content of the notification into the intent and only build it when it should be displayed and I want to refactor that later on.
My main problem is, how do I determine in the onReceive of my broadcast receiver if the app is currently showing to decide if a notification or an alert should be displayed?
Or is there an entirely different approach that might work better (for example using WorkManager)?
I think you can handle it on your BroadcastReceiver
public void onReceive(Context context, Intent intent) {
if (isForeground(context))
// AlertDialog
else
// Notification
}
public boolean isForeground(Context mContext) {
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).getTaskInfo().topActivity;
return topActivity.getPackageName().equals(mContext.getPackageName());
}
return true;
}

How to Send a Notification to MainActivity when user enters Geofence with Android Java [duplicate]

This question already has answers here:
Today is nth day of year [duplicate]
(6 answers)
Converting a Date object to a calendar object [duplicate]
(3 answers)
Closed 1 year ago.
I have an app that defines a Geofence location in an Activity called SearchSchool. I then implemented a BroadCastReceiver extension in another class to listen when user enters the geolocation and when he leaves it. I seem to have done everything correctly except the part where am supposed to send a notification to user from the BroadcastReceiverClass to the SearchSchool Activity which is an extension of AppCompatActivity class.
Here is the code in the SearchSchool activity that defines the Geofence builder...
public class Search_School extends AppCompatActivity {
PendingIntent geofencePendingIntent;
private GeofencingClient geofencingClient;
List<Geofence> geofences = new ArrayList<>();
//replace with real latitude and longitude of your geofence
//define the longitude and latitude of the geofence and radius
private double lat = 37.8136;
double lon = 74.0060;
private float radius = 100;
private long EXPIRATION_TIME = 10000;
#Override
protected void OnCreate(Bundle savedInstanceState){
geofencingClient = LocationServices.getGeofencingClient(this);
geofences.add(new Geofence.Builder()
.setRequestId("MyGeofence")
.setCircularRegion(lat, lon, radius)
.setExpirationDuration(EXPIRATION_TIME).
setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
//add a geofence to the client
geofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getActivity(), "Geofence added with success",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity(),"Geofence addtion failed",Toast.LENGTH_LONG).show();
}
});
}
//create a geofence request
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder =new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geofences);
return builder.build();
}
//Create a geofence pendingRequest
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (geofencePendingIntent != null) {
return geofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return geofencePendingIntent;
}
}
Here is the BroadCastReeceiver class for detecting when user has entered the geofence and should send a notification to SearchSchoolactivity when it occurs
package com.example.fastuniforms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofenceStatusCodes;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
import static android.content.ContentValues.TAG;
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
// ...
public void onReceive(Context context, Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceStatusCodes
.getStatusCodeString(geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
this,
geofenceTransition,
triggeringGeofences
);
// I havea problem creating a notification method in a non Activity class
sendNotification(geofenceTransitionDetails);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG,"An error occurred, try again in a little bit");
}
}
//i have a major problem here getting the details and sending them
private String getGeofenceTransitionDetails(GeofenceBroadcastReceiver geofenceBroadcastReceiver, int geofenceTransition, List<Geofence> triggeringGeofences) {
}
}
I have a problem with the two areas highlighted in the second class, please help me make the getGeofenceTransitionDetailsand the ``sendNotification` methods work Thank You

Java Android create canvas that will pop-up automatically at a specified date

I am developing a simple android app that manages events(An event contains a name, a description, and a date). For example: Somebody's birthday or something like that.
I want a canvas to pop up by itself at the specified date(even if I'm not in the application). I managed to create a Toast using AlarmManager and BroadcastReceiver which pop ups automatically at the specified date. But I can only write text to a Toast.
Is there any way to create a canvas that will pop up similar to a Toast?
My alarm manager:
AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
IntentFilter filter = new IntentFilter("ALARM_ACTION");
registerReceiver(receiver, filter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "It's your mom's birthday!");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,operation) ;
And my Receiver class:
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//I want to replace the Toast with a canvas.
Toast.makeText(context, intent.getStringExtra("param"), Toast.LENGTH_SHORT).show();
}
}

Android Geofences (Location Services) - How to trigger in-app events

I've been using the Google Location Services API with Geofences. I can successfully catch Geofence transitions and I receive notifications when I enter and exit Geofences. However, I do not know how to make a Geofence transition make GUI and Data changes to the app.
For an example, if the app is open, I want a small toast to appear. I also need my app to generate a new geofence and discard the old one (but so far I'm only using a set of hardcoded geofences).
But so far, I do not understand how I can interact with my app from the GeofenceTransitionsEventService.java (which is where I "catch" the intents and create the notifications).
Below is my code (now edited, but still not working):
GPS Activity
public class GPSActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private String mLastUpdateTime;
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
private final int MY_PERMISSION_ACCESS_FINE_LOCATION = 1;
private boolean locationPermissionGoodToGo = false;
public double latitude, longitude = 50;
protected static final String TAG = "MainActivity";
/**
* The list of geofences used in this sample.
*/
protected ArrayList<Geofence> mGeofenceList;
/**
* Used to keep track of whether geofences were added.
*/
private boolean mGeofencesAdded;
/**
* Used when requesting to add or remove geofences.
*/
private PendingIntent mGeofencePendingIntent;
/**
* Used to persist application state about whether geofences were added.
*/
private SharedPreferences mSharedPreferences;
// Buttons for kicking off the process of adding or removing geofences.
private Button mAddGeofencesButton;
private Button mRemoveGeofencesButton;
protected ResultReceiver mResultReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
// Get the UI widgets.
mAddGeofencesButton = (Button) findViewById(R.id.add_geofences_button);
mRemoveGeofencesButton = (Button) findViewById(R.id.remove_geofences_button);
// Empty list for storing geofences.
mGeofenceList = new ArrayList<Geofence>();
// Initially set the PendingIntent used in addGeofences() and removeGeofences() to null.
mGeofencePendingIntent = null;
// Retrieve an instance of the SharedPreferences object.
mSharedPreferences = getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
MODE_PRIVATE);
// Get the value of mGeofencesAdded from SharedPreferences. Set to false as a default.
mGeofencesAdded = mSharedPreferences.getBoolean(Constants.GEOFENCES_ADDED_KEY, false);
setButtonsEnabledState();
// Get the geofences used. Geofence data is hard coded in this sample.
populateGeofenceList();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
initReceiver();
}
public void initReceiver(){
mResultReceiver = new AddressResultReceiver(new Handler()) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Check result code and/or resultData and take necessary action
if(resultCode == 0){
makeToast(resultData.getString("FROM_GEOFENCE_KEY"));
mRemoveGeofencesButton.setText("Remove TESTT");
}
}
};
// if(mResultReceiver != null){
// mAddGeofencesButton.setText("Add Geofences NOT NULL TEST");
//}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
intent.putExtra(Constants.RECEIVER, mResultReceiver);
}
public void makeToast(String string){
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
#Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
}
// setCoordinates();
else{
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
#Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
mGoogleApiClient.connect();
}
#Override
public void onLocationChanged(Location location) {
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
mLatitudeTextView.setText(String.valueOf(location.getLatitude()));
mLongitudeTextView.setText(String.valueOf(location.getLongitude()));
latitude = location.getLatitude();
longitude = location.getLongitude();
// Toast.makeText(this, "Updated: " + mLastUpdateTime, Toast.LENGTH_SHORT).show();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
locationPermissionGoodToGo = true;
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
//setCoordinates();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
locationPermissionGoodToGo = false;
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void startGame(){
//mGeofenceList.add(new Geofence.Builder()
Geofence fence = new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this geofence.
.setRequestId("hej")
.setCircularRegion(latitude, longitude, 150) //radius in meters
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();//;
}
/**
* Builds and returns a GeofencingRequest. Specifies the list of geofences to be monitored.
* Also specifies how the geofence notifications are initially triggered.
*/
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
// The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
// GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
// is already inside that geofence.
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
// Add the geofences to be monitored by geofencing service.
builder.addGeofences(mGeofenceList);
// Return a GeofencingRequest.
return builder.build();
}
/**
* Adds geofences, which sets alerts to be notified when the device enters or exits one of the
* specified geofences. Handles the success or failure results returned by addGeofences().
*/
public void addGeofencesButtonHandler(View view) {
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
return;
}
try {
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
// The GeofenceRequest object.
getGeofencingRequest(),
// A pending intent that that is reused when calling removeGeofences(). This
// pending intent is used to generate an intent when a matched geofence
// transition is observed.
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
} catch (SecurityException securityException) {
// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
logSecurityException(securityException);
}
}
/**
* Removes geofences, which stops further notifications when the device enters or exits
* previously registered geofences.
*/
public void removeGeofencesButtonHandler(View view) {
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
return;
}
try {
// Remove geofences.
LocationServices.GeofencingApi.removeGeofences(
mGoogleApiClient,
// This is the same pending intent that was used in addGeofences().
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
} catch (SecurityException securityException) {
// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
logSecurityException(securityException);
}
}
private void logSecurityException(SecurityException securityException) {
Log.e(TAG, "Invalid location permission. " +
"You need to use ACCESS_FINE_LOCATION with geofences", securityException);
}
/**
* Runs when the result of calling addGeofences() and removeGeofences() becomes available.
* Either method can complete successfully or with an error.
*
* Since this activity implements the {#link ResultCallback} interface, we are required to
* define this method.
*
* #param status The Status returned through a PendingIntent when addGeofences() or
* removeGeofences() get called.
*/
public void onResult(Status status) {
if (status.isSuccess()) {
// Update state and save in shared preferences.
mGeofencesAdded = !mGeofencesAdded;
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded);
editor.apply();
// Update the UI. Adding geofences enables the Remove Geofences button, and removing
// geofences enables the Add Geofences button.
setButtonsEnabledState();
Toast.makeText(
this,
getString(mGeofencesAdded ? R.string.geofences_added :
R.string.geofences_removed),
Toast.LENGTH_SHORT
).show();
} else {
// Get the status code for the error and log it using a user-friendly message.
String errorMessage = GeofenceErrorMessages.getErrorString(this,
status.getStatusCode());
Log.e(TAG, errorMessage);
}
}
/**
* Gets a PendingIntent to send with the request to add or remove Geofences. Location Services
* issues the Intent inside this PendingIntent whenever a geofence transition occurs for the
* current list of geofences.
*
* #return A PendingIntent for the IntentService that handles geofence transitions.
*/
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
//DESTROYS EVERYTHING---> intent.putExtra("RECEIVER_KEY", mResultReceiver);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
* This sample hard codes geofence data. A real app might dynamically create geofences based on
* the user's location.
*/
public void populateGeofenceList() {
for (Map.Entry<String, LatLng> entry : Constants.BAY_AREA_LANDMARKS.entrySet()) {
mGeofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(entry.getKey())
// Set the circular region of this geofence.
.setCircularRegion(
entry.getValue().latitude,
entry.getValue().longitude,
Constants.GEOFENCE_RADIUS_IN_METERS
)
// Set the expiration duration of the geofence. This geofence gets automatically
// removed after this period of time.
.setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
// Set the transition types of interest. Alerts are only generated for these
// transition. We track entry and exit transitions in this sample.
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
// Create the geofence.
.build());
}
}
/**
* Ensures that only one button is enabled at any time. The Add Geofences button is enabled
* if the user hasn't yet added geofences. The Remove Geofences button is enabled if the
* user has added geofences.
*/
private void setButtonsEnabledState() {
if (mGeofencesAdded) {
mAddGeofencesButton.setEnabled(false);
mRemoveGeofencesButton.setEnabled(true);
} else {
mAddGeofencesButton.setEnabled(true);
mRemoveGeofencesButton.setEnabled(false);
}
}
}
GeofenceTransisitionsIntentService
public class GeofenceTransitionsIntentService extends IntentService {
protected static final String TAG = "GeofenceTransitionsIS";
private ResultReceiver mResultReceiver;
/**
* This constructor is required, and calls the super IntentService(String)
* constructor with the name for a worker thread.
*/
public GeofenceTransitionsIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}
#Override
public void onCreate() {
super.onCreate();
//Intent i = getIntent();
}
/**
* Handles incoming intents.
* #param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
#Override
protected void onHandleIntent(Intent intent) {
mResultReceiver = intent.getParcelableExtra(Constants.RECEIVER);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
this,
geofenceTransition,
triggeringGeofences
);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
sendToActivity("Sending to activity test");
//Toast.makeText(this, geofenceTransitionDetails, Toast.LENGTH_SHORT).show();
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, "R.string.geofence_transition_invalid_type");
}
}
/**
* Gets transition details and returns them as a formatted string.
*
* #param context The app context.
* #param geofenceTransition The ID of the geofence transition.
* #param triggeringGeofences The geofence(s) triggered.
* #return The transition details formatted as String.
*/
private String getGeofenceTransitionDetails(
Context context,
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);
// Get the Ids of each geofence that was triggered.
ArrayList triggeringGeofencesIdsList = new ArrayList();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}
/**
* Posts a notification in the notification bar when a transition is detected.
* If the user clicks the notification, control goes to the MainActivity.
*/
private void sendNotification(String notificationDetails) {
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(getApplicationContext(), GPSActivity.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(GPSActivity.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);
// Define the notification settings.
builder.setSmallIcon(R.drawable.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
/**
* Maps geofence transition types to their human-readable equivalents.
*
* #param transitionType A transition type constant defined in Geofence
* #return A String indicating the type of transition
*/
private String getTransitionString(int transitionType) {
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
return getString(R.string.geofence_transition_entered);
case Geofence.GEOFENCE_TRANSITION_EXIT:
return getString(R.string.geofence_transition_exited);
default:
return getString(R.string.unknown_geofence_transition);
}
}
public void sendToActivity(String string){
Bundle b=new Bundle();
b.putString("FROM_GEOFENCE_KEY", string);
mResultReceiver.send(0, b);
}
}
Here is my current error message which causes the app to crash
04-11 17:21:21.952 9528-9528/blueinteraction.mamn01blueinteraction W/System: ClassLoader referenced unknown path: /data/app/blueinteraction.mamn01blueinteraction-1/lib/arm
04-11 17:21:21.974 9528-9528/blueinteraction.mamn01blueinteraction I/GMPM: App measurement is starting up, version: 8487
04-11 17:21:21.974 9528-9528/blueinteraction.mamn01blueinteraction I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
04-11 17:21:21.983 9528-9528/blueinteraction.mamn01blueinteraction E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
04-11 17:21:21.984 9528-9528/blueinteraction.mamn01blueinteraction E/GMPM: Scheduler not set. Not logging error/warn.
04-11 17:21:22.008 9528-9528/blueinteraction.mamn01blueinteraction I/InstantRun: Starting server socket listening for package blueinteraction.mamn01blueinteraction on android.net.LocalSocketAddress#e9aec24
04-11 17:21:22.010 9528-9528/blueinteraction.mamn01blueinteraction I/InstantRun: Started server for package blueinteraction.mamn01blueinteraction
04-11 17:21:22.012 9528-9567/blueinteraction.mamn01blueinteraction E/GMPM: Uploading is not possible. App measurement disabled
04-11 17:21:22.138 9528-9572/blueinteraction.mamn01blueinteraction D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-11 17:21:22.196 9528-9572/blueinteraction.mamn01blueinteraction I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
04-11 17:21:22.199 9528-9572/blueinteraction.mamn01blueinteraction I/OpenGLRenderer: Initialized EGL, version 1.4
04-11 17:21:23.868 9528-9572/blueinteraction.mamn01blueinteraction D/OpenGLRenderer: endAllStagingAnimators on 0xb40e3280 (RippleDrawable) with handle 0x9f7bf6f0
04-11 17:21:29.767 9528-9572/blueinteraction.mamn01blueinteraction V/RenderScript: 0xaeca2000 Launching thread(s), CPUs 4
04-11 17:21:29.793 9528-9769/blueinteraction.mamn01blueinteraction E/AndroidRuntime: FATAL EXCEPTION: IntentService[GeofenceTransitionsIS]
Process: blueinteraction.mamn01blueinteraction, PID: 9528
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.ResultReceiver.send(int, android.os.Bundle)' on a null object reference
at blueinteraction.mamn01blueinteraction.GeofenceTransitionsIntentService.sendToActivity(GeofenceTransitionsIntentService.java:187)
at blueinteraction.mamn01blueinteraction.GeofenceTransitionsIntentService.onHandleIntent(GeofenceTransitionsIntentService.java:87)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
04-11 17:21:29.898 9528-9572/blueinteraction.mamn01blueinteraction D/OpenGLRenderer: endAllStagingAnimators on 0x9eee6280 (RippleDrawable) with handle 0x9f7bf670
I think these are the only classes I need to show you here for you to be able to help. Otherwise tell me what else to include.
You can use ResultReceiver to send data back to Activity or Fragment
https://developer.android.com/reference/android/os/ResultReceiver.html
its send method would allow to add resultCode and Bundle to send data back to class which implemented the receiver.
From your Activity or Fragment, you will pass an instance of receiver to the IntentService. Such as,
Intent intent = new Intent(context, GeofenceTransitionsIntentService.class);
intent.putExtra(Constants.RECEIVER, receiver);
activity.startService(intent);
Now in your IntentService class, you would get the receiver via bungle arguments and keep an instance in the class.
In your IntentService's onHandleIntent method, probably any where else you want to trigger the event, you will use the receiver to send the data back to the calling class. Something like this,
protected void onHandleIntent(Intent intent) {
ResultReceiver resultReceiver = intent.getParcelableExtra(Constants.RECEIVER);
}
ResultReceiver is Generic interface for receiving a callback result from someone. Use this by creating a subclass and implement onReceiveResult(int, Bundle), which you can then pass to others and send through IPC, and receive results they supply with send(int, Bundle).
So extend that class and create a new class in your app like this,
public abstract class AddressResultReceiver extends ResultReceiver {
public AddressResultReceiver(Handler handler) {
super(handler);
}
}
Then AddressResultReceiver might look something like this in your Activity,
protected AddressResultReceiver mResultReceiver = new AddressResultReceiver(new Handler()) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Check result code and/or resultData and take necessary action
}
};
I hope this helps.

Android Proximity Alerts Not Working

I'mm trying to create an app that allows you to set a proximity alert for marker when you click on it's info window.
googleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
public void onInfoWindowClick(Marker marker) {
LatLng clickedMarkerLatLng = marker.getPosition();
double lat = clickedMarkerLatLng.latitude;
double long1 = clickedMarkerLatLng.longitude;
Log.e("hello", "Output=" + lat + long1);
LocationManager lm;
// double lat=123,long1=34; //Defining Latitude & Longitude
float radius=30; //Defining Radius
lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Intent i= new Intent("com.example.sleepertrain5.ProximityReceiver"); //Custom Action
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, i, PendingIntent.FLAG_CANCEL_CURRENT);
lm.addProximityAlert(lat, long1, radius, -1, pendingIntent);
}
This is the code that calls the Broadcast Receiver
public class ProximityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
// The reciever gets the Context & the Intent that fired the broadcast as arg0 & agr1
String k=LocationManager.KEY_PROXIMITY_ENTERING;
// Key for determining whether user is leaving or entering
boolean state=arg1.getBooleanExtra(k, false);
//Gives whether the user is entering or leaving in boolean form
if(state){
// Call the Notification Service or anything else that you would like to do here
Toast.makeText(arg0, "Welcome to my Area", 600).show();
}else{
//Other custom Notification
Toast.makeText(arg0, "Thank you for visiting my Area,come back again !!", 600).show();
}
}
The above is the Broadcast receiver. None of this works and I can't figure out why. Any help would be great.
Use below code for Pending Intent:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
nId, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Where nId will be any uniq number that represent particular pending intent.

Categories

Resources