Background execution in Xamarin.Android - java

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.

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

Firing events on Android and Desktop Java when a database changes

I'm currently trying to work out what technology I can use to fire events remotely when a database changes, a realtime database of sorts. There is both an android application and a desktop JavaFx app that will communicate via the same database. When the data is changed from the desktop side, I'd like Android to update its data, and vice versa when changes occur on Android. Is there any method to achieve this without polling the DB for changes regularly?
I looked into Firebase and it seemed perfect, but lacked a desktop java library. Similarly, I have experimented with Amazon AWS Lambda and DynamoDB, and I can get a Lambda function to fire when the DynamoDB table changes. I can't however find a way for the Lambda function to update the Android/Desktop application that the data has changed though.
The JavaFx desktop application is a requirement of the project.
Apologies if this isn't possible, or if I'm overlooking a well known platform for this issue. Any help would be greatly appreciated.
No you not need the process of polling to achieve your results. To answer the first question, first the most flexible and plausible approach would to built a middle ware to intercept any changes.
Create a script to fire events whenever the data base changes. Wether by a time interval.
Second create server, a real-time server to fire such events to any client.
Third, the java fx client can use native observable for such a task. However i proposed going with a common listener, a perfect choice would be socket.io, there web-socket implementation available for android and vanilla java. For android client, whenever a data is inserted , transformed etc , use a broadcast receiver(local) to fire events in the to notify the server. Or use the socket.io connection to send events. Thats pretty much it.
Firebase + Cloud Functions for Firebase sound perfect for your use case. If the matter is pure Java support for Firebase, rather than Android, you might want to check out this question Get Firebase to work with java, not Android

Not sure what control in android should I use

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:);

background threading in android

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).

Categories

Resources