Android to update geofence regularly











up vote
0
down vote

favorite












I am reading through documentation from here.



https://developer.android.com/training/location/geofencing



It can update me location for my geofence when I run that service. Problem is I need to monitor continuously. So, I am thinking to do timer in my service and check my geofence regularly. But, I think it will definitely drain battery.



I don't want to monitor location and instead, I need android OS to update me whether user goes inside my geofence just like iOS. Is there any way to implement that ?



public class GeofenceTransitionsIntentService extends IntentService {
// ...
protected void onHandleIntent(Intent intent) {
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);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}









share|improve this question
























  • You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
    – Dhaval Patel
    Nov 15 at 4:24










  • Where shall I create geofence? In main activity or service? It is because user can kill app.
    – Khant Thu Linn
    Nov 15 at 5:01










  • I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
    – Dhaval Patel
    Nov 15 at 5:04










  • ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
    – Khant Thu Linn
    Nov 15 at 5:40










  • You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
    – Dhaval Patel
    Nov 15 at 5:43















up vote
0
down vote

favorite












I am reading through documentation from here.



https://developer.android.com/training/location/geofencing



It can update me location for my geofence when I run that service. Problem is I need to monitor continuously. So, I am thinking to do timer in my service and check my geofence regularly. But, I think it will definitely drain battery.



I don't want to monitor location and instead, I need android OS to update me whether user goes inside my geofence just like iOS. Is there any way to implement that ?



public class GeofenceTransitionsIntentService extends IntentService {
// ...
protected void onHandleIntent(Intent intent) {
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);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}









share|improve this question
























  • You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
    – Dhaval Patel
    Nov 15 at 4:24










  • Where shall I create geofence? In main activity or service? It is because user can kill app.
    – Khant Thu Linn
    Nov 15 at 5:01










  • I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
    – Dhaval Patel
    Nov 15 at 5:04










  • ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
    – Khant Thu Linn
    Nov 15 at 5:40










  • You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
    – Dhaval Patel
    Nov 15 at 5:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am reading through documentation from here.



https://developer.android.com/training/location/geofencing



It can update me location for my geofence when I run that service. Problem is I need to monitor continuously. So, I am thinking to do timer in my service and check my geofence regularly. But, I think it will definitely drain battery.



I don't want to monitor location and instead, I need android OS to update me whether user goes inside my geofence just like iOS. Is there any way to implement that ?



public class GeofenceTransitionsIntentService extends IntentService {
// ...
protected void onHandleIntent(Intent intent) {
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);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}









share|improve this question















I am reading through documentation from here.



https://developer.android.com/training/location/geofencing



It can update me location for my geofence when I run that service. Problem is I need to monitor continuously. So, I am thinking to do timer in my service and check my geofence regularly. But, I think it will definitely drain battery.



I don't want to monitor location and instead, I need android OS to update me whether user goes inside my geofence just like iOS. Is there any way to implement that ?



public class GeofenceTransitionsIntentService extends IntentService {
// ...
protected void onHandleIntent(Intent intent) {
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);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}






android geolocation android-location






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 1:54









shizhen

2,4963727




2,4963727










asked Nov 15 at 0:57









Khant Thu Linn

2,01042777




2,01042777












  • You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
    – Dhaval Patel
    Nov 15 at 4:24










  • Where shall I create geofence? In main activity or service? It is because user can kill app.
    – Khant Thu Linn
    Nov 15 at 5:01










  • I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
    – Dhaval Patel
    Nov 15 at 5:04










  • ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
    – Khant Thu Linn
    Nov 15 at 5:40










  • You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
    – Dhaval Patel
    Nov 15 at 5:43


















  • You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
    – Dhaval Patel
    Nov 15 at 4:24










  • Where shall I create geofence? In main activity or service? It is because user can kill app.
    – Khant Thu Linn
    Nov 15 at 5:01










  • I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
    – Dhaval Patel
    Nov 15 at 5:04










  • ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
    – Khant Thu Linn
    Nov 15 at 5:40










  • You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
    – Dhaval Patel
    Nov 15 at 5:43
















You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
– Dhaval Patel
Nov 15 at 4:24




You don't need to monitor geofence continuously. Create geofence and sit back, Geofence will notify you when you are in the geofence area.
– Dhaval Patel
Nov 15 at 4:24












Where shall I create geofence? In main activity or service? It is because user can kill app.
– Khant Thu Linn
Nov 15 at 5:01




Where shall I create geofence? In main activity or service? It is because user can kill app.
– Khant Thu Linn
Nov 15 at 5:01












I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
– Dhaval Patel
Nov 15 at 5:04




I am not sure about your app workflow. But maybe you can create geofence when your app starts. Once you create the geofence, even if the user kills the app, You will be notified.
– Dhaval Patel
Nov 15 at 5:04












ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
– Khant Thu Linn
Nov 15 at 5:40




ah okay. let me try. there is one thing I don't understand. Do I need to start GeofenceTransitionsIntentService ? Or mGeofencingClient will invoke GeofenceTransitionsIntentService automatically?
– Khant Thu Linn
Nov 15 at 5:40












You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
– Dhaval Patel
Nov 15 at 5:43




You don't need to start GeofenceTransitionsIntentService. You need to specify this service while creating Geofence. Geofence will invoke the service for you.
– Dhaval Patel
Nov 15 at 5:43

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310975%2fandroid-to-update-geofence-regularly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310975%2fandroid-to-update-geofence-regularly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents