Not sure what control in android should I use - java

Hi I'm new to android and I was hoping to get some minor non-coding help. Simply put I am continuously getting an image off from a server, but the problem is I'm not sure which control I should use. I know I can't use async task because it is used as a one time call and I can't use a service because I only want to run it on one activity alone. When the app closed or activity is changed the continuous process stops. Should I use thread, executor, future task or some other control?
Edit: Basically its going to be just like Async download image except it's going to call continuously while app is still on and the activity is the same as it is. Here is a sample of async download
http://javatechig.com/android/download-image-using-asynctask-in-android

For image processing you should most often use of of the various image processing libraries out there. Check out Picasso and Glide. They handle all threading and caching automatically for you.
http://square.github.io/picasso/
https://github.com/bumptech/glide

Get a image from service is about Android Network Programming .You can use HttpUrlConnection or HttpClient communicate with service,then you use JAVA stuffs,
like getting inputStream..Then use BitmapFactory.decodeStream() decode your inputStream and get a image from service.(Remember you must do network operation in child thread:)
Then use Handler let your bitmap show in your ImageView!
(I don't know if it can help you,forgive my bad English:);

Related

Cordova: Calling JS method from JAVA, when app is closed

Problem Statement:
I've a scenario where I want to call JS method from JAVA plugin side code. The catch is App is closed.
Scenarios
Plugin JAVA code calling JS calculate based on App Data
<========>
Do Action based output
on output data
App running or Background App Closed
WebView available WebView not available
We can call JS method from JAVA We can call JS method from JAVA by createing
temporary WebView instance
But can't use the method sitting with App code
Tried
Called JS method while app running or background
Created temporary WebView runtime and executed JS statements
I hope I'm able to explain the scenario clearly.
Can someone have any idea on this?
Thanks in advance.
I'd say you most certainly need a Cordova plugin, because this is the only way to execute native Java code and invoke Android/iOS APIs.
Also, Android and iOS will generally suspend apps in the background, so you need a plugin like this to prevent the app from sleeping. However, the stores could detect this and reject your app because they are against this type of behaviour.
Apps sent to background must be suspended to prevent battery drain, except apps where some kind of real time service is involved. As a side note, my S7 even suspends the WhatsApp live location sharing when sent to background, so if even they couldn't manage to keep it active... good luck with your app.

Multi-Part request in background

This is more a question of software that a programming question, so making a multi-part request making that with the app open makes the user wait a bit, I can do a background task but I'm trying to keep the app api in 21(Android), what do a make in app open or I make a background that works with the app close and send the values until the file is ended?
There is a lot of cases you can do.You can use: RxJava or Kotlin coroutines (if you use Kotlin). Also you can try to use Koltin flow (similar to rxJava) but it's experimental yet.
If you need to make requests to rest api one by one you can do it with coroutines. All you need it's:
Add "suspend" keyword to method
Start it on View Scope or somewhere else
For rest api calls I recommend using Retrofit. This library can deserialise responses to pojo (for example with GsonConverterFactory). Also you can use "suspend" there

Background execution in Xamarin.Android

I want to execute some code in background in my Xamarin.Android app. For example, make a HTTP request and do some actions on server side, get a response and update UI.
So, should I really use Android specific components like AsyncTask, IntentService, etc for that? Can I just run my code in Task.Run? Are there useful methods in C#/Mono to achieve my purpose?
You should use the C# model of asynchronous programming, it is used extensively in most Xamarin applications. Basically, your methods return "async Task" objects, which you can then "await" without blocking the UI thread.
EDIT
I also found some related information specific to Xamarin.

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