kandi X-RAY | ForegroundService Summary
kandi X-RAY | ForegroundService Summary
ForegroundService
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Handles the start foreground command
- Start button
- Initializes the instance
- OnDestroy destroy
ForegroundService Key Features
ForegroundService Examples and Code Snippets
Community Discussions
Trending Discussions on ForegroundService
QUESTION
I'm writing an Android location app and foreground service. The app gets location updates from the service periodically. I'm using Android Studio and writing the app in Kotlin.
The problem is that when the phone is rotated a new instance of the foreground service is created. This is demonstrated with Log outputs that show the count of location updates as well as the hash code of the service:
...ANSWER
Answered 2021-May-05 at 09:36After many hours of debugging I found the problem:
In MainActivity.onDestroy() I was attempting to stop my Service LocationService. The problem is that I attach a location callback to the Service and therefore the Service is leaked when the phone is rotated because MainActivity.onDestroy() is called which attempts to stop the service but can't apparently because of the attached Location Listener and Android seems to assume that it successfully stopped the service and so creates a new instance of it the next time around.
*** Seems to be an Android OS Bug ***
QUESTION
I have a small test App that with an Android GPS API map fragment. I use FusedLocationProvider. TarketSDK=29. Using Java.
As long as the app is active it works beautifully. On locationUpdates, I add a new point to the track and everything looks great and stays accurate. The goal is to track my hike, total distance and track and show it on the map. Works great.
As soon I lock my phone or loses focus, then the updates stop and I no longer get location updates.
Solution seems to be:
- Background Service (discouraged)
- Foreground Service
- PendingIntent
I have poured over the docs, StackOverflow, all examples/tutorials I can find, developer.android.com, etc. I have downloaded examples of the latter 2 from GitHub; they seem incredibly obtuse (probably just me).
- What are the dis/advantages of ForegroundService vs PendingIntent?
- How about a bare-bones example illustrating the min features of each to implement location updates while your phone is locked in your pocket or some other app is active? Just the template minimum.
- I need to save the locationUpdates that occur while my app is not active or phone is locked; in order to fill in Track when activity is restored to the app.
Some simple end-to-end guidance from my working app to something that will maintain locationUpdates and save the data would be great.
...ANSWER
Answered 2021-Feb-09 at 15:20Ok - I have answered my question in a roundabout way.
I had been Searching on "retrieving location updates when app is not active". This lead to the various solutions of background service, foreground service, pendingIntents, etc.
I eventually found that if you just start a Foreground Service with a Notification, even if your phone is locked or you switch active apps, your App continues to receive LocationUpdates; as the Foreground Service runs in the same thread and therefore activates your app code (if I understand the reasons why correctly).
So, I started searching on just how to start a Foreground Service. As anyone knows that has tried to figure this out lately, this has changed more than a couple times over recent versions. The online docs at developer.android.com are not up to date. You will spend a lot of time wondering why things do not work following these docs.
Eventually, with just searching on how to start a foreground service, I came across this simple and straightforward (non-youtube-video - don't you just hate those things) tutorial. https://androidwave.com/foreground-service-android-example/
I just added this code to my existing Mapping code that works when the app is active, and tested with locking the phone and putting it in my pocket and switching apps and doing the same. It appears to solve the problem.
Update: Added code to count number of location updates and average accuracy of each update holding the phone in hand, screen on and app active as the baseline. Phone locked, or App not active no difference in number of updates nor accuracy. Phone locked and in pocket, no difference in number of updates, but accuracy suffered by from an average of 10m to an average of 13m; to be expected I assume whilst in the pocket.
QUESTION
I have been working on a react native application where I have to fetch the geolocation at an interval of 10secs and update it to a server. I was able to do the same when the emulator was active/ in the foreground.
However, when the app is minimized, it doesn't work. I tried using expo task manager but the function doesn't seem to run when backgrounded.
What is the error here? I have attached my code and output.
I am using android 11 using an emulator and personal device.
Here is my code:
...ANSWER
Answered 2020-Oct-24 at 19:41To anyone who is looking at this later, please make sure that you test your app on a physical device. Emulator doesn't really support the task manager and background fetch properly!
QUESTION
I am trying to send a value to the redux store from taskmanager in react native expo. I am unsure where I am going wrong and how to properly extract the value and send it to the my redux store. In my case I am attempting to access the state
value from expo's taskmanager geofence api where state 1 means you've entered and state 2 means you've exited:
ANSWER
Answered 2020-Sep-23 at 00:25Move your TaskManager.defineTask logic into a separate JS file and initialize it on your componentDidMount and provide your action creator to it so that you can make sure it has access to your redux action and run in a redux scope properly.
QUESTION
I've just develop an Android app (minSdkVersion 23/ targetSdkVersion 29) that can connect to a BluetoothLE device to obtain data periodically.
Now, in the MainActivity (not the first activity), I do the following registering the broadcastReciever:
...ANSWER
Answered 2020-Jul-20 at 11:34The only working solution for working in Background is ForegroundService, other ones will be destroyed when your device will enter Doze mode. You can find more detailed information in this article, it describes all the obstacles in background working while scanning BLE devices.
QUESTION
java.lang.InterruptedException
error is thrown onDestroy
because of the Thread.sleep
in the Runnable
. I thought that the Runnable created a new Thread and allowed for sleeps?
ANSWER
Answered 2020-Jul-18 at 05:21Add thread = null; after Thread(runnable).interrupt(). This should work. If you want it to be killed when the user stops the activity you should try overriding onStop instead of onDestroy().
Hope it helps! Happy Coding:)
QUESTION
I'm building an app which will scrape some data from a website and shows a notification when some criteria are met.
Everything works well without problems when the app is open (because the WebView is being rendered) but when I close the app the WebView is disabled so I cannot use it to scrape data anymore.
The scraping code is inside a class called from a ForegroundService.
I've already looked on the internet but I'm unable to find a solution or a substitute to WebView, do you have any ideas?
I'm sorry if this question looks stupid to you, I've started to develop for mobile just one week ago
Below the JDMonitoring class which is called from the AlarmTask class
...ANSWER
Answered 2020-Jun-14 at 00:18[BETTER-FINAL-SOLUTION]
After several hours I've discovered Android WebView which does exactly what I need (I'm developing this app only for Android)
I've written this Browser helper class
QUESTION
In my app, there is a login activity first and a home activity. After logging in using volley and passing parameters to home activity in an intent, I'm able to start a foreground service that keeps the app running in background with notifications and getting back to home activity by clicking on the notification with the help of pending intent.
Now, I'm searching for how to open the app from main menu and accessing directly home activity with the pending intent of the foreground service. Maybe should I pass the parameters of the pending intent to the login activity and check them to redirect to home activity, but i'am stuck with this and don't understand.
Here is the login activity Page :
...ANSWER
Answered 2020-May-24 at 08:51The solution is to use SharedPreferences.
QUESTION
I keep getting these logs even when I called the unbind method after ranging. Wonder what might cause the issue?
The code is pretty much the same as the Ranging Example Code on Android-beacon-libary-Samples The only difference is I'm using a ForegroundService instead of an Activity.
...ANSWER
Answered 2020-May-20 at 16:43The library already has an easy built-in way to set up foreground service scanning that takes care of many of the issues you are seeing. See here for details.
If for some reason you don't want to use the above, you can certainly roll your own foreground service, but it will make managing the lifecycle more complicated. This is tricky stuff to get right.
In general, to cleanly shut down the library from scanning you need to do a few things:
- Call
beaconManager.stopRangingBeaconsInRegion()
- Call
beaconManager.unbind(beaconConsumerInstance)
Note that the call to unbind
is asynchronous. You do not want to be calling bind() / unbind() rapidly.
Finally, if you are going to the trouble to roll your own foreground service, then you might want to consider whether you want the library to be using its default behavior of using the Android Job Scheduler to perform scans, which is limited to once every ~15 minutes. It may be appropriate to simply let the library's own scanning service run to allow more frequent scans. You can configure that (before calling bind() only) with beaconManager.setEnableScheduledScanJobs(false)
QUESTION
Let me explain the scenario.
I have a task I need to schedule in every 10 minutes.
This task needs both network and disk resources EVEN if the app is in the background AND even if the Battery Saver has kicked in.
I tried AlarmManager, JobScheduler, and ForegroundService.
Then only one that seems to work when Battery Saver kicks in is the ForegroundService.
In xamarin I have started a Foreground Service in the MainActivity.cs like the following.
...ANSWER
Answered 2019-Jun-18 at 07:12To stop a foreground service I usually use:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ForegroundService
You can use ForegroundService like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the ForegroundService component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page