I am a research student who just started the android programming for 3 weeks and I am trying to write an App which extracts data from accelerometer from the phone and writing it on my phone. My problem is that I would like to run my App (taking data from accelerometer) all time when the phone is up and running. What I mean is that my App has to run all time when somebody is calling, facebooking and so on. Is that possible? I would like to get some references.
What I mean is that my App has to run all time when somebody is
calling, facebooking and so on. Is that possible?
So for long-tasks you can use AsyncTask or Services. If you want to execute some task and it have to run also when its not connected with any Activity(for example Music player, RSS which still run also after release from memory by memory manager), you should decide to use Services and also you can combine Services with AsyncTask.
Services are strong tool but work with them is not trivial. You are able to execute only one Service in time and only one Service can running, one instance, one copy. This all is not free so you have to be careful because when you implement Service too dirty, it may cause premature exhaustion of battery.
There is more approaches how to start Services but you have to read some tutorial and guides.
I recommend to check this: Services, ServicesDemo - Using Android Services, Android Service Tutorial, Local Service | Android Tutorial for Beginners
Also have look at AsyncTask, Android Threads, Handlers and AsyncTask - Tutorial
You can use Services. Look here
If you want your activity of your app to be closed, but still a process should be running at background doing your desired work, then you can use Service, or IntentService (Use intent service, in place like, you want an update after certain period of time).
Related
My android java app has Offline Video feature like the YouTube app. I do not use android.app.DownloadManager because it does not support pausing the downloads(or as I know).
I have been using a third party download manager library(tonyodev.fetch2) and it does the work done but the problems are:
How can I make the download independent of the app closing/opening etc and also being able to control it when required from an Activity.
If I use a Service for the above problem, what do I use, IntentService or just normal Service keeping in mind I have to know about the ongoing downloads and control it(pause/stop/resume) from any activity.
I have been using android worker class which extents ListenableWorker but I cant seem to make things work as required.
I have been struggling with this for a long time now any help is welcome, or a totally new approach for this particular problem.
The background services are not encouraged any more from Android Oreo (>25). I want the socket to keep connected in my chat application even when the app is closed.
How can I implement new changes to android 26?
Some people says, use JobIntentService
Some people says, use JobService
Some people says, use JobScheduler
Some people says, start service as Foreground Service
Any help would be appreciated.
I faced the exact same problem working on a chat application so I know your pain. Our conclusion was:
you don't keep a connection alive 24/7, if you need to deliver a
message to an user that has no connection alive, send a push message
via Firebase.
If you want to keep a connection alive in background, you will face many problems. The first one, targeting Oreo, is that if your app is in background (definition of "background" in this context is here) it won't be allowed to run except for small time windows.
You can definitely use JobScheduler to run periodic tasks, they won't be executed at exact intervals or times to reduce battery usage (which is good) but it won't help you in keeping a connection alive. At best, you can use JobScheduler to periodically pull messages from you server. In order to use JobScheduler you need to create a JobService class.
JobIntentService is a new class introduced in API 26 of support library. It is a replacement for IntentService, it will run as a JobService on android API 26+ and as a Service (similar to IntentService in the sense that it will execute code in a background thread) on older APIs. On Oreo its background execution will still be limited so it won't help you in keeping a connection alive.
Using a foreground Service can really help you reducing the likelihood of the process being killed, but, you will need to display a permanent notification. It doesn't sound like a good solution for a chat app.
If you still think that having a 24/7 connection alive is a viable option, you need to consider also doze mode. You could ask the user to whitelist your app to run even in doze mode but you should have a very good reason to do that. Again, you would face the other bg execution limit in Oreo.
Another issue you will face is other apps. There are resources management apps that will aggressively kill other apps in bg to reduce memory and battery usage. For instance, I cursed this one quite a bit.
Another issue is created by android. When the system is running low on memory, it will start killing processes of apps in bg. There is a an order in which they're killed, if I recall correctly should take into account last time it was in fg and current memory usage. Not the worst of the problems but still, it happens.
Then, if I still haven't convinced you in giving up the idea of the permanent connection, let me share with you yet another problem you would face. Some vendors implements extremely aggressive policies when it comes to killing bg processes, so that they're battery will last longer. For instance, Xiaomi.
Last tip, unrelated, but it took us a while to figure this out so I'm going to share it. If the user force stops your app from settings, your app is dead (that is, "stopped state") until the user actively launches it again, it won't even receive Firebase push messages.
If your server is configured on XMPP, then it would be easy for you.
Actually, there isn't any need to keep the socket alive at all the time. This is very expensive for the battery and I'm sure you don't want that.
Case: You are working on a Messaging app.
If your socket is broken, then the client will receive the message in the form of Google FIREBASE notification. And the moment your client will receive a notification from firebase, just enable the socket, and you'll be back on track.
The reason I suggested you to use XMPP is because XMPP maintains a queue of undelivered/offline messages. And when your socket is connected again, you simply pull the offline messages from the server.
I don't think this will help you out, but this may make a room for some another idea for you.
I want to create an application in Android that will show the devices list of applications and then allow the user to select which apps they want to be restricted access to for a certain period of time.
I am aware there is an Android Application named "AppBlock" but i don't know how this works.
You select the app, the period of time and then it doesn't allow you to open the app.
You cannot actually block start of another application.
The only way that I see it is possible: you need to save the list of applications Info in your app that need to be blocked with the time when it need to be blocked. Implement a service that runs "forever" and detects started applications.
Refer to this answer about how to do it. On each detection you should check if application present in your database and if the time now says it need to be blocked. If it is - close the application. Refer to this answer for learning how to do it.
That is global architecture I think you should follow.
To Build App Block u need the following things
1-Accessibity services (AS)2-Forground Services (FS)
BY AS you will be able to stop activity that you have in your bloker list
FS will alive your application context that help to find block app
i made an application which keep the screen awake, and it works fine until it gets killed by the system.
How can i keep my app running in background?
I see a lot of apps that uses notifications to stay running and avoid being killed, but i don't know how to implement it.
Can someone help me achieve this?
You can use the Service API for this, specifically the startForeground(int, Notification) function call. There is an example provided in the API documentation that I have linked to.
Services or AlarmManager is what you are looking for I think, depends on what you want to do..
Use Android Services.You can use the following methods:-
To call a Service which doesnt return any value back use - startService()
To call a Service which return back values use - bindService()
Its good to be cautious when using any Sensors to run in the background as it might eat up you battery fast
I want to make my app run in the background like a process that runs always.
I need it because I want to get locations update for GPS every 2 minutes (longitude, latitude) and to use the information in a method.
For that I need for the app to be running when the phone is asleep or not in the UI of the app in other words I need the app will run always.
I'm sure that there is a way to make it , thanks anyway for any answers :)
This was just the first google search result I found:
http://www.vogella.com/articles/AndroidServices/article.html
The answer here is to use a service, if this tutorial is lacking there are 6.4 billion others.
We have something like this, but it is made up of several parts.
Firstly you will want your code to run (and be registered in the manifest) as a Service
You will probably also want to request android.permission.RECEIVE_BOOT_COMPLETED so that you can write and register a BroadcastReceiver that gets notified by android.intent.action.BOOT_COMPLETED action and its onReceive method kicks off the service.
In our case we also have a front-end activity which also pokes the service to make sure it is running, but it's been a while snce I checked to see if this was still required.
Our service is nearly empty and onCreate immediately calls a custom Handler which then manages the 'ticks' which wakes the Handler and fires a Runnable if there is work to do, but this is where my code diverges from yours. In our case we only attempt to update the GPS location when the service 'ticks' (usually every minute) and there is work to do. It usually only performs a couple of dozen operations per client per day so I can't really advise on how it will impact battery usage.