How to do task in the background? - android

is there a method that I can use to do code when the app is closed? I need to refresh the database and check for incoming polls from other devices.

You'll want to use a service. Here's a starting point for what you'll want to learn. There are many examples and tutorials to be found with a Google search for "android service example"

Related

Implementing a Sticky Service in android/flutter

I need to add a native sticky background service in a flutter application, in order to achieve 2 things:
Starting at boot time and running in background indefinitely
Exchange data with the main Dart activity, in a message passing fashion
However, I cannot find any kind of useful documentation. It seems that for now, you have to choose to go completely native or giving up using low level features and focus only on the UI (until someone pulls a specific plugin out of the hat).
Thus, my question is the following: what is the easiest way to achieve this sort of integration, starting with a basic flutter project ?
Thank you
Make a Sticky Service using native Android service.
The easiest way to exchange data with the main Dart activity is to use deep links or intents.
Note: if you explain more why do you need that, I think I may be able to give you a better solution.
While you can register a BroadcastReceiver to be activated at BOOT your idea of having a service "running in background indefinitely" is highly discouraged in recent version of Android.
Therefore what can you do is have a Broadcast receiver registered in Manifest that will be activate when BOOT completes (See https://developer.android.com/guide/components/broadcasts for a sample) and from here you can use the available API to schedule work. Check out this link to see what from available API best suite you needs.
While there are more complicated solutions you will find that simply sending Intents between components will do the job.
You can use the method channel
Docs : https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-swift-tab

Run code in background, Unity?

I would like to call a method every 10 seconds while app is closed. I have read that I have to create a service but I don't understand how. It would send information to a webserver (only personal uses).
Thanks guys!
If you want to send information to your webserver only while the application is running, you can use a thread within unity.
If you want some code to keep running, irrespective of whether or not your app is running, you need service. This should get you started on how to write a service.
You can write a small plugin in android native code, and include it's aar/jar in your unity project. Then you may send a message from your unity app via JNI to start the service. Totally depends on how you want to implement it though.
I have found a way of doing it. You can call a method via OnApplicationQuit() and this will run when the app gets closed. I have used this to get changes from my firebase database and send a notification when a child is added. It works. In this case you would have to subscribe to a method and this will run even if the app is closed. Then unsubscribe to this method via the OnAwake() method.
Check this
< https://developer.android.com/training/run-background-service/create-service>
You have to create a service but probably its very difficult for you.

call periodically a .net webservice from android

I have an android app that calls a simple .asmx web service hosted in an asp.net application to get data and display it.
I'm using ksoap2, and it's working perfectly, in the main activity I'm calling the webService. but my problem is that I want to call that .asmx web service every 30 minutes and get the data to display. I've searched and found the timertask class but I'm not sure if it will fit with what I want to do.
any suggestions?
Many thanks
You should have a Service to do this for you, Create a service and in that service you can schedule a thread to call your webservice every 30 minute even if your application isn't running. Here is the good tutorial on how to use service to let you get started. And this is a useful link about How and When to use Service. Please consider accepting the answer by clicking on left green Tick icon.
Have a look at the Scheduled Task service: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html
It will let you set up a recurring event every x seconds, and will keep running that task until you tell it to stop.
I found it to be pretty easy to set up.
Sorry for answering my own question, thank you for your help. I've found a solution, I followed this tutorial and it worked perfectly.
Hope this will help.

How can I create a android service for listen a server?

is there any way to listen a Server action using a service?i want to check server message for life time and do actions based on server message,please post a sample code
I think what you looking for is a long time running background service, check out this SO question.
Here i have example of using service, but it's doing some other thing. You can work it out..
http://maephv.blogspot.com/2011/08/android-notification-in-your.html
But remember, that when screen comes off, services freeze. I don't know yet how to fix it, but maybe "background process" is word to google

first android service

I creating a small application that will basically use a background server to send data over HTTP. I dont know how to create services. Secondly there will be a couple of activities in my application. I want the activities to display a Context Menu when data becomes available. How can i do both. I have search for a while but the code i keep getting dose not seem to run on 1.6 api. How can i create the service and how can my activities listen to updates so that when a update is available they display a message.
NOTE: I do not need help on the HTTP part and the server part only creating the service and my activities listening to updates.
Kind Regards,
Give the Service docs a very good, thorough read.

Categories

Resources