Android 8 Geofences on killed app - android

With the transition to android 8 I encountered an issue that geofences in app are not working anymore when app is killed.
I implemented geofences as per android developers guide, so no reason to show any code snipplets.
With some small modifications and explicit broadcast receiver I was able to receive geofence notification when app was running and when app was in background.
Still no luck with case when app is killed. If anyone had made transition to android 8 and has working geofences please share your experience.

You will have to change the PendingIntent that the geofence triggers to a BroadcastReceiver instead of a Service. The system will no longer let a Service start when your app is in the background.
Also noted in this answer.

Related

How does Setting Up Notification when user leaves the app work?

I am new to coding. I am using Android Studio with Java. I spent the last 3 hours looking for how to set up a notification display at the top when the user leaves the app so they can always click on the notification when they want to return to the app but I couldn't find anything with that action I need.
The app tracks the users GPS and I want them to be able to always be aware that when the app is running it will continue to track their GPS. So my idea is to have a notification window pop up whenever they leave the app so they can be aware the app is running in the background.
Any help will be much appreciated! I am building this app on my own as a passion project and I am taking the Udacity Google Android course. I finished the course early and working on this app for fun. We didn't learn about Notifications or GPS tracking but I am doing my best to learn new things. Thank you for your patience and help!
My app code is here for easy viewing:
https://github.com/PoetryHorizon/eloteroMan2
Use a foreground Service. For any background logic, you should be using a Service anyway, and in Android Oreo and above, you're required to have it be a foreground Service, meaning it posts a Notification.
You can then add a PendingIntent to that notification set to reopen your app. See Android's Notification documentation on how to set a PendingIntent. You'll want to use PendingIntent.getActivity(), where the Intent you pass is new Intent(someContext, YourActivity.class).

Geofence transition PendingIntent blocked by the OS on Android Oreo

This only happens on Android Oreo. I'm using Play Services 11.4.2.
I'm registering geofences using the GeofencingClient and the addGeofences method with a pendingIntent to an IntentService that is handling geofence transitions.
It looks like the intent sent by the play services is blocked by the OS in certain conditions.
The system logs the following:
Background start not allowed: service Intent { cmp=my.app.id/my.package.struct.GeofenceTransIntentService (has extras) } to my.app.id/my.package.struct.GeofenceTransIntentService from pid=-1 uid=10154 pkg=my.app.id
as soon as I add a geofence in the following situations:
when I add it after the device boots
when I add it after the app was swiped
In both occasions the app is actually already running in the background (since I'm able to run the code that adds the geofence) because I listen to the PROVIDERS_CHANGED, BOOT_COMPLETED.
This is caused by the new Android Oreo background service limitations.
You have to change the PendingIntent from using a Service to using a BroadcastReceiver.
See this CodeLab for further information and example code.
In addition to switching to a BroadcastReceiver as mentioned in the accepted answer, I'd like to point out that although you will still receive BOOT_COMPLETED, you will not be able to receive PROVIDERS_CHANGED any longer.
PROVIDERS_CHANGED is an implicit broadcast and is not in the list of broadcast exceptions.
BOOT_COMPLETED is in the list of broadcast exceptions which is why you'll still receive it.
If you want to learn about an alternative approach to get the hook when location services is toggled, please see my answer to a related question for more details.

How to keep track of geofences when app is not running

I am using geofence service in my app and using exactly the same sample as given in the google documentation.
Doing some research i found that the Intent service is killed when the app is killed. I want to receive the geofence notifications even when the app is not running. How can i do that.?
I have looked for the examples using broadcast receivers but they are old and use classes which are deprecated.

Google Cloud Messaging - Will it work when app is not running

I have looked on similar threads however couldn't find a definitive answer.
For android 3.1+, if an app is force killed it doesn't receive broadcasts.
Force killed stops all running services and proccesses.
Does this mean if my app doesn't have a running service and is swiped out of recent apps then it will not receive GCM notifications?
Or does this only apply to when the force stop button is actually pressed.
Maybe you know the (deprecated) version of GCM. There we had to implement a WakefulBroadcastReceiver service which was started automatically when reveicing any GCM notification. This service had to "wake up" your app and in turn is able to start any of your own services.
This of course is still valid for the most recent GCM API-version but there you have to extend GcmListenerService which also is called by a WakefulBroadcastReceiver.
For detailed information on how to implement this please refer to Google's code sample.
To say it short - yes it will work if it is implemented correctly.

Keep service running in background without showing notification

I'm developing a messaging application and I need my service to keep running in background even if the application was closed but without showing a notification in navigation bar and start when the phone is started and restart itself when it is closed for any reason, I know this question has been asked before but I can find nothing to do this, I just want it to be like whatsapp or facebook or bbm services, thanks in advance.
I need my service to keep running in background even if the application was closed but without showing a notification in navigation bar and start when the phone is started and restart itself when it is closed for any reason
That is not strictly possible.
You are welcome to return START_STICKY or START_REDELIVER_INTENT from your service's onStartCommand() method. Android will still terminate your process due to old age and low memory conditions from time to time, but Android will eventually restart those services. What percentage of the time your process will be running will depend on a variety of factors, not the least of which being how much system RAM the Android device has.
Better yet, you are welcome to use Google Cloud Messaging (GCM) to deliver your messages to your app. This way, you do not need a service running all of the time. Your app can get control when a message arrives, do some work for that message, then go away.
I just want it to be like whatsapp or facebook or bbm services
BlackBerry Messenger uses startForeground() and has an icon in the status bar as a result.

Categories

Resources