Thread updating wallpaper stopped when leaving app? - java

I want to program an app which updates the device background every few ms (this way i want to display a gif; first idea I had any better ones?) and I wonder if this would work with a Thread or a Runnable which updates the wallpaper every few ms.
But won't the Thread or Runnable be stopped/destroyed when I leave the app?
Do you guys have any better ideas for displaying a gif file?
Backgrondinfo: I want to use a gif as wallpaper and a picture as lockscreen and the playstore apps can't handle that. (Got root proviliges to place picture as lockscreen)
Also it's a good practice for me ;)

To do this type of things, There's a separate concept called Service . Service is process which runs even after a Task is destroyed() . To know more about Service's check out devlopers.google.com

Related

Keep file observer running in background even if the app is closed

I have created a file observer object and it is working fine. But my requirement is to keep the file observer alive even if the app is closed manually by the user. I could have used service to keep it running but after android O that thing is not allowed anymore. Now you may say use job scheduler but I want fileObserver to fire the event as soon as the file/folder is updated.
is there any way to do so?
thank you in advance
currently you have only one option for doing this - use ForegroundService. its working just like usual Service, but it have to keep sticky Notification, which informs user that your app is working in background. as far as I know there is no listener firing when any folder content change...
you can also fire your code from time to time with AlarmManager or WorkManager, but still it may have some delay, may not fire in exact time and/or may drain battery (system will punish your app, flag as "battery drainer" and suggest user to force stop/uninstall, you can also get banned in GP Store)

How to periodicaly record screen from a background service using Android API MediaProjection?

Hello I'm building a weak AI (a bot) app on android but I'm fairly new to this.
Context: The app is/will be composed of an UI to start/stop the bot and modify his settings, screen caputure service taking screenshot at fixed intervals (let's say every 5 sec for example), an image recognition module(OpenCv) and a touch simulation service(Instrumentation class, MotionEvent class).
Apart from UI obviously, every module should be abble to run in background once the bot is launched.
Question: What is the most efficient, senseful way to make a service that can capture screen at fixed intervals from background?
I looked for MediaProjection API doc and demos then I started to make an IntentService that use MediaProjection to record screen and a Timer + scheduled TimerTask to save bitmaps but i'm a bit lost. Here is what I have so far:
Removed
I feel I'm doing it wrong. Can you help me to figure out how to do this with explanations, advices, snippets, tutos or anything helpful please?
Edit: Indeed this is much more simple to achieve this using Runtime to execute adb shell commands.
Plus this allow to make image processing on the computer which is faster

How to have a GDK app stay active after the glass is removed from the head

I am developing an app which requires the gdk program to continue running as-is even after the device is removed from the head. Currently the program is paused when this happens seemingly by default, but I need the program to stay running because it is constantly uploading video. The desired result is that the program will continue to run and upload a video stream even if the glass is removed from the user's head.
What can I do to change this behavior?
thank you.
You probably want to perform the long running upload in a background service. Android's activity model makes it such that you can't really depend on the life of the activity to always be in the foreground and you need to use a service for tasks that shouldn't be paused.
Also if you have an activity you want to return to when you put Glass back on, ensure you specify android:immersive="true" within your declared <activity>. Without this, your activity could be completely destroyed when the screen turns off.

Download multiple files in background in Android

Basically I'm trying to make a little app for watching offline content. So there's a moment where the user selects to download the contents (and the app should download about 300 small files and images).
I'd like to show the user how does the process go if he enters the proper activity. Showing a list of all the files, telling what has been already downloaded, in progress or waiting for download.
My problem is that I really don't know what approach to take for achieve this. Since the download should last until finished I imagine the solution is an Service, but whats best? an IntentService, a Bound Service or an Standard Service calling a startService() for each download? And how can I keep my objects updated for displaying them later? should I use a database or objects in memory?
Thanks
I would suggest using AsyncTask class, it allows you to easily move time consuming code(like downloading files) to a different thread. This will keep your app responsive, while giving you the ability to update your UI in the process.
It's hard to be more specific without having more details about how exactly you want your app to behave. Are the downloads only going to happen when the app is running or in the background as well?
You could use Asynctask or implement a ExecutorService with custom policies and send to it the download threads.
You need to keep a reference to the AsyncTask or a Future object respectively inside of a collection if you want to give the oportunity to the user to stop downloads.
Of course, you need to call startService each time you want to download a new file.
Service onCreate only is called if service is not running and onStartCommand run each time you call startService. In onStartCommand you run a new thread for download a new file.
You can bind service with an activity and each time that your downloadsActivity is created you show the state of downloads implementing a custom Adapter. Service only finishes when you call activity.stopService or service.stopSelf

Widget update results in ANR

I have a simple app that reads internet resource and displays the information in a widget or in listview activity in form of imageviews and textviews.
In addition to downloading the data from internet it also shows it in widget in a ViewFlipper.
When I add the widget to the home screen, it fires onUpdate immediately, downloads the data from internet and updates the widget. This works just fine. Log shows onUpdate and dataDownloaded with about 3 sec apart.
On the next update (phone has gone to sleep mode), the update doesn't happen and this is what my logs report.
onUpdate is called.
dataDownloading is called, but after 20 seconds after onUpdate has been initially called. I assume this is because the phone was in sleep and it takes time to initialize networks sockets etc.
After this, I get the ANR log entry and widget update doesn't happen, process is practically dead, widget stays on screen and doesn't respond to manual updates from within activity, which otherwise works when no ANR exception is thrown.
I'm looking for a possible solution to this. I was thinking about calling all the downloads in a different thread (from within the AppWidgetProvider, possibly using AsyncTask), store data in SQLite or local storage and doing the widget update (no downloads, just reading the data from SQLite and local storage) on the next onUpdate call. This would make the application/widget process more responsive and not fault into ANR.
Is this threading approach a bad practice? Is there an alternative? Should I use service instead? I'm inclined not to use a service, unless there's a lot of pros for it.
Sorry for the wall of text :)
Edit: From the docs http://developer.android.com/guide/practices/design/responsiveness.html
Android will display the ANR dialog for a particular application when it detects one of the following conditions:
No response to an input event (e.g. key press, screen touch) within 5 seconds
A BroadcastReceiver hasn't finished executing within 10 seconds
Threading is the only way to safely do network access on Android. So, yes, you'll need to use something like an ASyncTask or IntentService. Note that a plain Service won't be much help, since that runs on the main thread.
If you are performing a network request then you need to do so either within an AsyncTask or in a Thread/Handler combination. Here are some links to help:
AsyncTask
Painless threading
Threading
Designing for responsiveness
Thread documentation
Handler documentation
Using IntentService and a database backend is the proper way to do it I guess.
But what you never should do is performing such background tasks when the application is not active. Please only download data if your app is in foreground!
As for the widget you should use the "updatePeriodMillis" attribute. The Android system makes sure this is only executed when the widget is visible.
For more hints look at the usual location:
http://developer.android.com/guide/topics/appwidgets/index.html

Categories

Resources