Im developing a desktop app in java that when the app is closed by the frame I have an event call FrameClosing that make a function when user close the windows , the problem is when the app is close by the task manager or when the user turn off the windows and the app is running, i was trying this.
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("app was stoped");
}
});
but it's only works if app is closed by the Frame. I was reading about socket my app could can communicate with a website and if the communication between them I could detect that the program was closed but I dont really too much about it
I just want to detect when my app stop running
Your code is correct. If it is killed via the task manager though that is end of story. It is killed before it can run System.out.println. If that wasn't the case you would be able to do all sorts of stuff after the user said they want to kill it.
This answer is talking about C++ but the second point is the important part. If the program is killed via Task Manager or kill -9, there's really not much you can do about that. The program doesn't have access to that level and you'd have to have the program be looking for that specifically.
Rather than try to deal with that, have you considered designing the app so that an unexpected shutdown isn't a deal breaker? For example, doing intermediate logging while the program is running. You could use that to determine if the app was prematurely shutdown when its run next and do whatever is necessary before it starts working.
You could also look at this thread. Its talking about killing via the command line but it might help you in some situations.:
Logically, SIGHUP (terminal hangup) should be raised.
The equivalent of SIGHUP is provided through the callback you register with
SetConsoleCtrlHandler. Your callback function will be called on an
arbitrary threadpool thread with dwCtrlType = CTRL_CLOSE_EVENT. You've got
5 seconds to clean-up, you cannot cancel the close.
Related
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...
I have an application with call activity, when I finish action in activity the activity is still running in background device. Now I want to stop the activity and not allow it to run in the background when it is finished. I'm also using killBackgroundProcesses but it's not working.
public void KillApplication(String killPackage) {
ActivityManager am =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
android.os.Process.killProcess(runningProcess.pid);
am.killBackgroundProcesses(killPackage);
}
I just call finish(); and then I use the function KillApplication("mypackage");.
From the looks of it, your processes should be terminated. Are you thinking that the process is still running because the application still appears in the 'recents' list? If that's the case, take a look at the "Close application and remove from recent apps" question.
It explains how to remove an application from the recents list by using an Intent flag.
What do you mean by run in background?
The best practice is calling finish(), make sure all the process are stop, and let the OS fully kill the app when it need the resource.
But if you want to force kill, you can use
System.exit(1) instead of finish().
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.
I'm trying to engage myself into the Libgdx OpenGL framework. I've used LwjglApplication for creating some simple apps that render boxes, some meshes and some textures. I came across Aurelien Ribon's app that creates rigid Box2D bodies. He used the LwjglCanvas to integrate with Java's Swing. I tried making one myself, I created a JFrame then added the LwjglCanvas. Then set the JFrame's default operation on close to EXIT_ON_CLOSE.
However everytime I close the application, this logs to my console:
AL lib: alc_cleanup: 1 device not closed.
I don't know what it means and it's not doing me any harm. I just want to know what it means. According to LwjglCanvas docs:
All OpenGL calls are done on the EDT. This is slightly less efficient then a dedicated thread, but greatly simplifies synchronization. Note that you may need to call stop() or a Swing application may deadlock on System.exit due to how LWJGL and/or Swing deal with shutdown hooks.
Where should I bind the LwjglCanvas.stop(), should I add it to the EventDispatchThread queue or should I bind it to the JFrame.addWindowListener?
And what does "AL lib: alc_cleanup: 1 device not closed" really mean?
Thanks a lot!
AL.destroy(); must be called before the application shuts down. this call is normally included in a finally block, but if the application calls System.exit(0), finally blocks are no longer executed.
You should try always closing your app with the piece of code below:
Gdx.app.exit();
I usually put it in my pause() method, because there was no reason for my app to run in the background.
This static method tells the framework that your application is planning on closing and will thus release all resources effectively. The error you're getting is because the JNI interface has loaded an OpenAL library, but the System is closed before that library is properly unloaded and released. I had the same problem as you did, but this solved it. As stated in the JavaDoc, the exit() method:
Schedule[s] an exit from the application. On android, this will cause a call to pause() and dispose() some time in the future, it will not immediately finish your application.
The AL lib is the "audio library" used by Libgdx (one of the OpenAL variants).
I believe this message just means that the audio library is cleaning up some (in your case just one) audio streams/handles for you. If you see this at exit, its harmless as all resources will be cleaned up by the OS.
If you internally cleanup your audio before exiting, the message should go away.
For more details, look for alc_cleanup in here:
http://repo.or.cz/w/openal-soft.git/blob/HEAD:/Alc/ALc.c
I have an app that downloads a file from an FTP server upon tapping the file in a listview. Once the tap is received the download starts. This causes my app to become unresponsive, in logcat it is giving me the debugging info from the server and it is downloading just fine, the issue is this. If the download takes too long say over a minute or so android thinks the application is froze and asks if the user wants to Force Close or Wait. How do I prevent this?
Use worker threads, refer to Handling Expensive Operations in the UI Thread, http://developer.android.com/guide/appendix/faq/commontasks.html#threading
since the download is taking so long the UI thread is doing nothing. Android assumes your program is stuck and offers to kill it since it isn't making any "progress." Use another thread to download anything.
Everything that takes even a small amount of time needs to happen in a worker thread. Methods in the main thread need to return as quickly as possible, or else your app will become unresponsive.
yeah, you should use a different thread to download the file, I would use something like a loading dialog while the file is been uploading,