Java external display connected callback? - java

I'm writing a presentation app which is supposed to present on the 2nd display in extended mode.
For that I want to have a "Start presentation" button which reflects the actual state of external display connections: it should be gray and non-clickable if no external displays are connected. And it should be enabled if there is at least one connected.
I have seen the GraphicsEnvironment class but it only has getXXX methods. It's inefficient because I will have to check the state every few seconds and the state updates not immediately.
Is there a callback method on display connection or something like that for this purpose?
Thanks
Shuo

Related

Is it possible to toggle between 2 GUIs from the same Android Activity using a button?

I am writing my first android application that consists of a single activity. The activity creates a server, listens for data, decodes and proceesses the data, and displays it. I would like to display the server data two different ways, toggling between them with a button press. (default mode and debugger mode). The server needs to continue running at all times. What is the most simple way to do this?
Mode A: The default for the application. It will just display Icons and relevant information to the user from the messages recieved by the server. Contains a button to toggle mode B.
Mode B: this mode should be triggered when the user taps a button from within mode A. It will hide the information from mode A, and display more in depth information such as raw hex data, and additional information from the message as plain text. Contains a method to toggle back to mode A.
This is a pure front end, you can have a fragment placeholder in the activity and switch back and forth between a couple of fragments, each fragment should have a different XML UI layouts as you need your GUI.
Also, pack your layout views from a ViewModel in order not to trigger the server while transitioning between fragments.

How to restore previous application after my application is put in background on Android

I need a way to restore previous Android task when the current task (my application) is put in background using either back or overview button. For example, a video is played by youtube app when a SIP call is received. The softphone task is brought to foreground, then the call is answered. Once the call finishes, by pressing back or overview button youtube app is shown and the video continues playing. An example of such Android application is Linphone. I would like to know how this can be achieved programmatically.
As stated by others and here, Android handles it automatically for you. But if you need to add anything explicitly when going/coming to/from the background state then you can also override onSaveInstanceState() and onRestoreInstanceState() methods which will be called accordingly.
As your activity begins to stop, the system calls the onSaveInstanceState() method so your activity can save state
information to an instance state bundle. The default implementation of
this method saves transient information about the state of the
activity's view hierarchy, such as the text in an EditText widget or
the scroll position of a ListView widget.
To save additional instance state information for your activity, you must override onSaveInstanceState() and add key-value pairs to the
Bundle object that is saved in the event that your activity is
destroyed unexpectedly. If you override onSaveInstanceState(), you
must call the superclass implementation if you want the default
implementation to save the state of the view hierarchy.
#Override
protected void onRestoreInstanceState(Bundle outState) {
if (outState != null) {
Crashlytics.log(1, "FormActivity", "Method:onRestoreInstanceState, Msg: saved instance is not null");
if (outState.containsKey("record")
&& Session.getCurrentRecord() == null) {
Session.setCurrentRecord(
gson.fromJson(
outState.getString("record"),
Record.class
)
);
}
if (outState.containsKey("user")
&& Session.getCurrentUser() == null) {
Session.setCurrentUser(
gson.fromJson(
outState.getString("user"),
User.class
)
);
}
}
super.onRestoreInstanceState(outState);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
Session.setCurrentRecord(record);
outState.putString("record", gson.toJson(Session.getCurrentRecord()));
outState.putString("user", gson.toJson(Session.getCurrentUser()));
super.onSaveInstanceState(outState);
}
Source 1 Source 2
This is done automatically by the android system.
Now why you may not be able to notice this behavior for your app.
You may be launching your app from the app launcher. Which means that you already put all other apps (except the launcher) in background. Now depending on the launcher settings you may go on page from where you launched the app or home when you press back button.
When can you observe this behavior
If your activity is launched from background service, e.g. broadcast receiver
If your activity is launched by clicking on a notification button
basically when your activity is created without killing or putting other apps in background, you will get back to the same app when your app is closed.
Exception - If you use home button all apps go to background and home screen appears.
hope this helps.
here is the official documentation on how to preserve the UI state:
https://developer.android.com/topic/libraries/architecture/saving-states
if the user configured the phone to always kill activities in background or they have limited resources then you have to handle that, but in some cases (your activity wasn't killed and remained in memory) as Mayank answered the system will do it for you.
getting a call from phone app will interrupt your app (System-initiated UI state dismissal)
what you should do as suggested by the documentation above:
In the section: Managing UI state: divide and conquer
Local persistence: Stores all data you don’t want to lose if you open and close the activity. Example: A collection of song
objects, which could include audio files and metadata.
ViewModel: Stores in memory all the data needed to display the associated UI Controller. Example: The song objects of the most
recent search and the most recent search query.
onSaveInstanceState(): Stores a small amount of data needed to easily reload activity state if the system stops and then recreates
the UI Controller. Instead of storing complex objects here, persist
the complex objects in local storage and store a unique ID for
these objects in onSaveInstanceState(). Example: Storing the most
recent search query.
so In your case have a view model that stores the Url and the video time when the call got received
and I would also store the same info in the instanceState using the proper life cycle hooks
here is a good SO thread with example on how to use the savedInstanceState :
Saving Android Activity state using Save Instance State
it has old and new answers, you may want to read through it to get a sense of how things changed overtime
basically the three bullet points above are the recommended strategy by official documentation

Check if user computer's window is idle (Java)?

I want to create a notifications application for desktops which would show notifications/popups only when user is idle. By idle I mean when user can see his/hers desktop wallpaper and no program is blocking a single pixel of the desktop - http://prntscr.com/feorvd.
If someone knows how to better express what I want you are welcome to do edit the topic.
EDIT: I don't want for a popup to show up in the middle of a movie or a game ect. I don't want to be forced to shoo it away because it is blocking content that I want to give all my attention to.

Delay dynamic GUI changes

Activity description :
The main idea is to have an sms sending list and when user clicks the 'Send' button, the broadcast receivers start to receive and GUI changes like highlighting the 'Sent/Not sent' member list, updating progress bar , counting etc
Problems : Activity runs for hours and it's very important to save the exact same GUI change in sync with the user's action on the phone
However I have a few problems and th
The battery is dead/app crash.
Solution : Saving each 'send' state to file and loading it the next time when the user runs the app
The user hits the back/stop button or has an incoming call / Just wants to surf the web.
Solution : Maybe keep running it in the background? by using transparent activity ?
*NOTE : its not about the 'save' state or 'restore' state because everything depends on the broadcast receiver
Ideal way could be
do the backend logic with broadcast recievers + intent service and log the data in backend
Use the activity to just display the UI state based on the saved data.

Incorporate a timeout option when user click the upper right "x" on the window versus a button.

I built a Swing application that works with an MSAccess database. I have various buttons to click when clicked--they will disconnect and unlock the database.
Sadly, this is only in a perfect world where users will actually use those buttons and not the little red "x" on the upper right. When some users click that, the database stays locked with a file extension '.ldb' for those of you unfamiliar with MSAccess.
I need to avoid that. Is there any function that I can implement? Like a timeout? I looked up the
DriverManager.setLoginTimeout(10);
method, but that seems to be for login attempts. Is there something for my criteria?
Thanks.
PS: Sadly, i can't use any other database.
Sadly, this is only in a perfect world where users will actually use those buttons and not the little red "x" on the upper right.
See Closing an Application. You can create an Action that can be invoked when you use a button, menu item or when the user clicks on the "X" (close) button.
Simply configure what happens when the "x" is pressed using a WindowListener.
This is done easily using two methods for the JFrame (frame, in the examples below) that displays everything:
1. frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // by default pressing the "x" will do nothing.
2. frame.addWindowListener(new WindowAdapter() { // However, when the "x" is pressed...
public void windowClosing(WindowEvent e) { // this method is called,
... // and you write the body here to allow your program to respond appropriately
}
});
Eventually you'll want to call frame.dispose() to finally close the window.
You can implement additional methods in the body of theWindowAdapter inner class to respond to different window events... see the WindowAdapter interface for methods that can be implemented.
1- The KB5002099 patch deployed in Client computers recently, introduce a bug as a side effect of a security fix.
Here in the link is the description of this patch -> https://support.microsoft.com/en-us/topic/description-of-the-security-update-for-office-2016-december-14-2021-kb5002099-10670400-427f-4819-8de6-abd11e73100b
Inside this description, you will find the Know issue of this update, and the solution (KB4484211) How to get and install the update
Databases on network share can't be accessed by multiple users in Office 2016 (KB4484211) (microsoft.com)
2- Select download update for Office 2016, This update is available only for manual download and installation from the Microsoft Download Center.
https://support.microsoft.com/en-us/topic/databases-on-network-share-can-t-be-accessed-by-multiple-users-in-office-2016-kb4484211-88a51f7f-f7dd-2d9c-0b96-b7fca0867a4f

Categories

Resources