How to trigger a method to start at a certain? - java

I need a piece of code to start whenever it it 11 AM everyday. I do not want to use windows task manager or any other external things to start my app.
I need my app to trigger my method whenever it is time, 11 AM.
I have the following questions:
1) Is there any "Time Listener" (similar to action listener) that would do that
2) Is it ok if my app is always runs without closing

As others have suggested, you can use the Timer class in java (java.util.Timer) or for finer control the Quartz library (that library uses the Unix cron style).
And yes it is ok if your app runs without closing. You can run it at the background but you might be better adding notification icon in the taskbar to ensure the user can find your app when they need to.

Related

Java Splash Screen Application

I am running into an issue where it takes a really long time (10 seconds or so) to get through my ANT setup and running my java application. This will delay the splash screen for the jvm by at least 10 seconds to show up. Unfortunately, I can not move away from ANT to start the application due to certain constraints. What I would like to be able to do, and was wondering if anyone had a better solution, is to create a small splash screen application that would have the splash screen as an argument to the JVM so it will show up quickly and then use Runtime.getRuntime().exec("wscript....) to launch the other application. My concern is how to kill off the first application.
What I was thinking of doing is using jps within the other application to get the PID for the class that kicked off the application and then kill the Process (the first application would also have a timer to avoid it staying around if the second application did not start for some reason). I should say that there is a constraint that the main application (the second application) can only be run once and I have a bind solution to avoid this. I was wondering if there is a better way to terminate the first application. Should I use RMI or another way to tell the application to exit? If possible the less networking the better.
Wouldn't the ant Splash task do the trick?
This task creates a splash screen. The splash screen is displayed for the duration of the build and includes a handy progress bar as well.
in your first application (splash application) create a server socket with port XXX and listen for incoming messages, and apiece of code to terminate itself (e.g. System.exit();) when a message is received, and in your second application (when it is up) send a message to port XXX.
you also can implement a similar scenario using files. (1st application periodically checks a specific file exits and when it founds that file exists it terminates, 2nd application generates that specific file when it is up...

Run background code all the time when Android device is up

I want to run some code (mainly record system logs) in the background every certain time. This code runs all time when device is up. I have an app to control the start and stop of this recording code. I have tried putting the recording code in the service, but I found that the service always stops when app exits. This is not what I want. This function needs no notification.
BTW, this function is only for my custom Android system. So I have enough privileges such like system, driver or root. But I still want a way that is "high“ enough and affects the system least. So some normal java code is best, the kernel c/c++ modification to the custom OS is my last choice.
Thx.
Make a service class and give the permission to the maniefest
Here is the example...
https://developer.android.com/training/run-background-service/create-service.html
and do your stuff in the service, you can set time interval for that.

Detect package launch android

I need to detect if an application is launched and I've read that there's nothing in APIs to achieve this.
Someone suggested to give the app the permissions to read logs and keep polling them, but this solution isn't working since JB.
I also found that since I just need to know if the on top application changes I could do the following:
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
String str = ((ActivityManager.RunningTaskInfo)am.getRunningTasks(1).get(0)).topActivity.getPackageName();
But I need to know which way would be the best one to do this continuous polling, and - of course - if there is any other way to do this.
You could use accessibility service
http://developer.android.com/reference/android/view/accessibility/AccessibilityEvent.html and caputure window change event
As apps are used by people and people need time to read the info on the screen, it is not usual that the foreground app is visible for less than several seconds.
If you are trying to detect what apps the user has launched, then having a background service that polls every second or so polls the list in the way you indicate, then it should work fine and not deplete the battery either as the polling will be stopped as soon as the system goes to sleep.

How to Realize an Update Service is Running in the Background?

I'm currently working on an app for the Android OS that displays some data. To ensure this data is up-to-date, it is required that the app fetches updates from a remote server from time to time.
As the data does not change very often, this update should be carried out once per week. I want to give the user the ability to choose the weekday and daytime of the update (and optionally disable this feature completely).
The thing is: this update should be carried out even when the user is not using the phone at this moment, even when the phone is currently sleeping and even when the phone has been rebooted recently and the app hasn't been started yet.
The first thing I thought of was a remote service that starts at system boot, determines the time when to run the update, sets a timer and then waits/sleeps for the timer to fire.
Now, I was told I should rather use alarm timers or some kind of handlers... the more I read about this topic, the more ways to do this seem to exist.
Now, I'm a bit lost which method is the best for me... so here is what I need:
I want to execute some code at a time that is specified.
This timer is used to trigger the execution of code 7 days in the future. (i.e., every week at a given weekday and time)
The code should run WITHOUT waking the phone up if it is "sleeping" (screen dimmed).
When running the code, no activity should be started. i.e. no app pops up on the screen.
The code that is executed should fetch some data from the internet. If at this time no internet connection is available, the timer should be set to sleep 30 minutes and then try again.
After completing the code execution, the timer will be set for the next interval, which will be 7 days later.
The timer should be started at system boot, e.g., if I reboot the phone, the timer should determine the next date to execute the code and schedule the timer. This has to work without ANY user interaction! (i.e. without the app being started itself)
When "sleeping", the thread/service/timer/whatsoever should not consume any system resources if possible...
What i need is pretty much a simple unix cronjob.
I think anyone here knows "newsrob" for android? What I want to realize is pretty much the same as the newsrob-updateservice.
Android Service + Broadcast Receiver + Alarm Service will solve your purpose -
Your service will be invoked from BroadCast Receiver and In Broadcast receiver you should register for various events - BOOT_RECEIVER , ACTION_USER_PRESENT , which will take care of your ALARM reset and update task.
Thanks.

android - How to check that the Process is alive or not?

I want to check whether the process is still alive or not through programmatically ,Can I do that I am trying to do it by process name in onCreate method but the issue is that the onCreate method is called always .When I check that in onCreate method I always get the process name and I can not kill the current app and switch to previous one.
Can anybody help me ?
Thanks in advance.
I'm not sure I understand why you need this. It sounds more like you are wanting to save your state between application changes (your app going to the background).
On the Android, there are no two programs running at the same time, basically (there are services, but those are different). Once your user goes back to the main screen, if they "launch" your application again, it will go to the first activity defined, unless you override some functions to restore the previous state of the application.
There is no "the previous one" to go back to.
Unless you have seriously messed around with your manifest, there will be only one copy of your application (in one process) at a time.
Refer to this article in the Android documentation, specifically the Processes and Threads section:
Application Fundamentals
When the first of an application's components needs to be run, Android starts a Linux process for it with a single thread of execution. By default, all components of the application run in that process and thread.

Categories