If you encounter a problem when listening to the Android Home key, listen to the androidhome key
Problems with switching the source with the Home Key of the Android TV listener
The function is to press the Home Key, focus on the TV window, and press the Home key again to switch the source to another source. In the display of another source, press the Home Key to narrow down the source view and display the current source.
It is easier to solve the code for this requirement. Implement a BroadCastReceiver and listen to the Home Key. However, according to the implementation of the HOME key, the system will switch to the current Launcher. Therefore, when you click the HOME Key in the current source, the process is as follows:
OnPause-> onStop-> onRestart-> onStart-> onResume.
This is a function that is exposed to rewrite, but it actually hides a very important detail. When onRsume is executed, the system should asynchronously switch the View of avticher, that is, display the View of the current default Launcher. Therefore, when switching the source, this time period synchronization may cause problems. That is, when the focus is on the TV window, press the Home Key
① Switch the android system to the current Launcher,
② Switch the source to TV.
Or ② --> ①, you cannot enter TV. At this time, the current Launcher's View will not be re-painted. Therefore, it cannot be seen that the source has been switched and the process has been switched back.
Previously, logs have been written in various cyclic functions, and the check is normal (so it is assumed that the View is re-painted asynchronously ). Then, view the Log of the entire android system. To redirect data in the command line:
Adb logcat-v-f time> f: \ adblocat.txt.
In this way, you can view the log of the entire system and find that the current Launcher is switched in ActivityManager. Therefore, this problem is solved. The method is very simple, that is, to force the system to switch the Launcher in front of the new thread in BroadCastReceiver and sleep for a short period of time, and then convert the source after (I .e., ① --> ②), so it is OK.
In summary, when switching between multiple applications (source) and external applications (jar), you need to read the system log more frequently. The problem has been viewed. Understand the core controls. when an event occurs, think about the implementation mechanism as much as possible to investigate and solve the problem!