I have an application which has many activities that logins towards a web service. However my client want this application to log out after a certain timing of inactivity. Which includes screen off or no user interaction. I used the timer before but it will only work on a single activity and if i were to set the timer for 2 activities, 2 timers will be started and i'm not able to reset the previous timer when it moves on to the next activity.
I've thought that if i create a java class and extends Activity and all activities shares the same timer. Which means each time I need to start a timer, I will call the method on the java class.
Please advise or give suggestions on what actually should be done
Related
How would I got about implementing a function that starts a 10 second timer countdown when a user switches app and the if the user switches back AFTER the countdown ends, return some logic so the application know the user has exceed the timer? Is there a way to differentiate switching app with turning off the phone screen?
I.e. what do i need do in Android to implement the following?
User run apps
User switches out of application
App checks if its a) a task switch or b) a phone lock.
If its a), start a timer.
If timer ends, set some logic.
User returns to application. App displays a message based on logic in 5.
I'm trying to design an application that does two different tasks in parallel
each task start executing when the user presses a button in the activity "so I have three Activities two of them should do some tasks and the third one should collect the result".
When the user presses the button on the activity it will invoke a thread and then will Load the next Activity
In the third activity I have a button "I called it Send Button " that should stay inactive or disabled until all threads finish their work so at that moment it will be enabled and this activity contains plain text to show the result from these two threads and when the user presses this button it will send the information in the plain text to a web site.
My questions are : how can I run Threads in a different activity and send their result to another one? and how can I make that "Send Button" disabled until all threads finish their work.
I tried to make a while loop using a global variable in the onCreate method in the Third activity but it crashes the application and the activity didn't start up
You have two options to do a long running background task:
Using a thread which does your background work, with an singelton to access the results
Using a services which does the calculations
Depending on your actual use case the service is the first choice.
Use an Activity only when you need user interaction to your application. Else you can invoke both the threads using AsyncTask for your jobs(If you want the system to handle the worker threads for you. And unless you do not do any config changes on the fly -> performance hit).
Please keep in mind that, you should use AsyncTask only when the background jobs run for dew seconds. If you have a long running job, then use a service with a custom thread.
Hope this helped.
You can use AsyncTask. AsyncTask is one Class which is used to perform background processes without having threads. You can easily check the process is completed or not. Check this Tutorial. This can help you to understand how to run background process using an AsyncTask.
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.
I have a service that runs periodically using a timer to invoke itself, but should not run when the screen is off. When the screen on event is fired, the service should run, but only if it's past when the timer would have fired.
Right now I still run the timer continually, but have the service do nothing if the screen is off. I can also run the service via a broadcast receiver when the screen turns on - but this runs the service every time the screen is turned on, instead of only when it's past when the timer should have run. Recording this state in the service doesn't seem to work as Android will kill the JVM for the app in between executions.
What would be the cleanest/correct way to implement this type of behavior?
So there are a couple of intents that you can listen for, Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON. However, these don't work as manifest receivers, they need to be explicitly registered.
See this answer: https://stackoverflow.com/a/9478013/1306452
In all scenarios, it's going to involve a long running service in order to listen for those events.
One thing to keep in mind is that these intents only listen for when the device becomes non-interactive and vice versa, not specifically when the screen goes off (see the description on the Intents).
The best way for you to achieve this behaviour would be to listen for when these intents with a long lived service, started with START_STICKY to help guarantee that the service is running. The service can register a receiver for the SCREEN_ON and OFF events, and when it gets these events either do nothing if the timer has not elapsed, or continue if it has.
This won't be nice on your battery life, and what ever you are doing it doesn't sound like it's going to be a pleasent user experience. You might also want to step back and see if there's another way around this obstacle (my 2 cents).
Hi I am starting to look into android development and I couldn't find a good tutorial on snooze function. Here goes my question:
I have a simple alarm clock that I'd like to implement snooze. I have a AlarmActivity pass an intent to start AlarmAlertActivity. However, I when the snooze button is pressed, I want to call finish on AlarmAlertActivity. I have the snooze method written in AlarmAlertActivity using timer. However, when the AlarmAlertActivity class is finished, the timer no longer runs. I don't really want to do another intent to go back to AlarmActivity, because there might be multiple snoozes. Any help is appreciated!
You want to implement this functionality as a Service, not as part of an Activity. From the docs:
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.
You can design your AlarmAlertActivity to interact with the service (starting it if necessary). The service can then broadcast a message when the snooze goes off.
It's hard to provide any specific advice because you have not provided any details about what you're doing. However, the code on this thread might provide some guidance.