making an android activity run without ui - java

I have a startingActivity on Google-glass
I want to run it sometimes silently,
meaning it will run, communicate with the server,
get and send data - but the user won't see the app.
The user will see a static card from time to time,
but basically can be in a context of another app.
my tries:
I have thought to create a service instead of my
startingActivity,but there are too many things that relays on the main UI views
If I comment out the setContentView(), all my code that refers
view.findViewById() will fail. no? and besides the user will see a
black screen instead of silent run which i desire.
any other solution for silent run, but yet running the
startingActivity fully?

Related

Perform an Action when the app comes to foreground

I am developing an app which has to perform some background work. For that reason, I am using a Service. Now what I want to achieve suppose the user start the background work and while the work is loading then the user has an option to either minizine the app or wait till the loading is over.
After the loading is over I want to open another Activity. Now my issue supposes the user starts the loading and minimizes the app then when the loading is over the user has not yet returned to the app then if I start the Screen without even the user having my app in his view then the user might get interrupted with his work.
So what I want is when the loading is over, I want to only open if my app is visible to the user and if the app is not visible to the user then I want to wait till the user return back and only when the user returns back I want to open the Screen if the loading is over.
Now what I have thought is I should have a boolean which will track whether the app is visible to the user. On onStop I will set the boolean value to false and onStart I will set the value to true. And again onStart I will check if the loading is finished and if yes then I will open the Screen.
But I want to know whether there is a better way to achieve this? If yes then how. The reason I am looking for a better way is that I want to write a clean code for my app which might avoid bugs and crashes.
That is exactly the scenario LiveData and RxJava are for. Your activity will get the data only when the activity is visible. Your Viwemodel will provide your live data to the activity only when your activity is available and it's lifecycle aware. You can also consider using WorkManager if your app needs to continue to work even after your user closed your app, even if user restarts your app. It also comes with Constraints to optimize your work based on Network, Battery life...and provides livedata for your Viewmodel to consume.

Android activity LifeCycle, whitch method to run while loop when app is runnig?

I have created an app that is searhcing for bigger and bigger primes and saves them in a textfile. Right know im letting the user to click a button to make the app search for bigger primes and save the to file.
Istead of clicking i want the app to run a while loop in the background and do the searching and saving by it self without any user interaction.
Is the onStart(); a good method to put the while loop in so it runs in the background while the app is running?
Also should i use te Runnable interface to dynamically show the user wich prime is found and saved to the file?
Thank you in advance!
You can use Service if your while loop will do a lot of work or consider using AsyncTask (a few seconds at the most.).
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use (here)
AsyncTask enables proper and easy use of the UI thread.here
As mentioned #EpicPandaForce, if you're doing a CPU intensive(mp3 for eg.) consider using an IntentService because it is executed on another thread.

Exiting app after completion of all crouton toasts

I am using Crouton as alternative to native android toasts. I have to display 5-6 croutons one after the another and then call System.exit(0) to exit my app.
The problem is, I see first crouton for 1-2 seconds and the app just exits! The rest croutons aren't displayed at all. This is because when the code is being executed the undisplayed croutons are added to queue. And when it comes to System.exit(0) it exits the app without displaying those queued croutons. I have tried searching for solution for this and came across a solution in which I have to create a new thread, then sleep it for time = sum of durations of necessary croutons and then call System.exit in that thread. But then if I have more or less croutons in another situation then that becomes useless.
So can anyone think of a solution?
Why do you have to quit the app?
In general, in Android, applications should not quit, and show not provide a way for the user to quit them. Quitting an app is handled by the system when the user navigates away from it and goes back to the home screen.
If your application must quit (for example, because of an unexpected condition it can't deal with) and you want to make sure the user sees the information, then the best approach would be to use an AlertDialog to display the information.
So, long story short: revise your UI... if you're sure that this is the right way to do it, then simply don't call System.exit(0). Just show the toasts and then stick around. Eventually the system will decide to quit your app when memory is needed.

How to maintain Server Socket through Activities?

In my application I decided to make a server and client through Sockets (just testing my idea for now).
So I made two AsyncTasks (for the server and client) an on my onCreate (depending on user action - as I said I'm just testing, for now just with buttons):
new createServer().execute();
or
new connectClient().execute();
Question is, everything works good, but the app where I want to implement my idea has many activities, so the app that is serving the data, when leave the activity where the createServer Async Task is, the client (obviously) loses the connection.
Is there a way to maintain the server running "all time", and allow the user (with the "server app") to navigate through the app without affecting the connected clients? Because even if everytime I leave an activity I create the Server, the clients will lose connection anyway.
Really appreciate any suggestion.
EDIT: For the purposes of my test application, I need the system to be working only in foreground, no background, so no need of service creation.
Your problem is with correctly managing the lifetime of your server. First you'll have to decide what you want that lifetime to be. Clearly you've figured out that you want it to be broader than just the activity level, but do you want it to run in the background or only when your app is in the foreground? If it is only when in the foreground, you can get away with not creating a service, but instead keeping a reference to a component from your application object. However, if you want to keep your server running even when your app is in the background you do need a Service.
Update:
Here's an example of what I meant by "keeping a reference to a component from your application object":
Create a custom Application class.
Add a field to hold a reference to your server AsyncTask
private AsyncTask mServerTask;
Add a method to it to start your server AsyncTask
public void startServer() {
mServerTask = new createServer();
mServerTask.execute();
}
Probably add another method to stop your server
To start your server from an activity you can now do something like:
CustomApplication app = (CustomApplication) getApplication();
app.startServer();
Of course you'll want to do more to properly manage the server like making sure you don't start it twice, etc. and you might decide to use threads instead of AsyncTasks but this should give you an idea of how you can have an object with a lifetime beyond that of an activity.

How to use Androids location package in a service separate from the User Interface thread?

I have a small Android app that I have been working on that logs GPS data to my SD card in a GPX file. I currently have a main Activity that starts a Service to do all the background work. The service is kept in the foreground in the notification bar to make it the least likely thing to be killed by the OS. Currently I am requesting location updates from the service at the maximum frequency to get the most accurate route. The problem I am having is my User Interface is acting slow/strange. Correct me if I am wrong, but what I have concluded is that I have too much going on in the main thread of the app. My next thought is to try and move the service performing the acquiring and logging of data to a separate thread. I am new to Java/Android so the whole topic of interacting with separate threads is hard for me to wrap my head around. Initially in research I came across IntentServices, which are supposed to make threading easier, but from what I read these don’t seem to mix well with the Android location package because they don’t run long enough. I feel like I am running in circles with internet searches on this topic. I desperately need some guidance on how to achieve the following features for my programs service:
Separate thread from Main Thread
Fetching and storing of data must be the least likely thing to be killed by the OS and run indefinitely once started (don’t worry about battery I will have the device plugged in to power while running the app)
Eventually I will need the ability to interact with the User Interface
Thanks for any help you can offer!
this is a common problem that i have accomplished a lot on
in the launcher or main() ( what Android is calling an Activity ) you do as little as possible ( which amounts to saving the the refs they give you and maybe setting a few other things as long as you are there ) and do ^not^ drop in to a long-running activity
A Service is exactly what you need but instead of trying to pump it into a "hold on to it" state what you do is implement checks for nulls and handle as needed -- trying to "fix" a machine to make it run the way you want here actually involves rescinding you hold on the main thread and letting it go as fast as consistent with the Applicaton's general constraints.
To do this you can simply write a Service - reading everything available - then extend that service and implement Runnable then you run the constructor on that code from the Activity Constructor and do new Thead(yourClass).start(); in the onCreate() checking for Thread.isRunning() before starting it again ...
Service will have an onCompletion() call in it somewhere - it will go through an interface
All this is done in Android in something like start activity for result then you just to the UI stuff in that call or sorta figure out a way for the GUI to get called somehow at some time then check to see if Service is done an so report in the gui

Categories

Resources