Android: How to control the home button

久未见 提交于 2019-12-19 03:45:43

问题


We're trying to provide an application to the mentally and physically handicapped daughter of my neighbor that let's her use an Android tablet as a Talker, i.e., she presses a few big buttons and the devices generates speech. The application is basically a WebView and an additional object in Javascript used to perform and control the speech generation, plus some logic to handle the orientation changes. Html files are generated offline for her specific layout of the talking items. We've also added some music playing and picture viewing facilities to make the device more appealing to her.

Problem is that the home button brings her back into the madness of the Android launcher screen, and that on the test device (Archos 70) the home button is not a physical button but rather shown on the touch screen itself, making it too easy to accidentally hit it.

So I'd like to return to the Android launcher only by pressing a sequence home, back, home with no other action in between.

Can I achieve this by making my application itself the launcher? How can I then get back to the original launcher upon the home, back, home sequence? It seems that this goes deep into the innards of Android, huh?

The only clue I found so far is Overriding Home button for a Car Home replacement app, but this is rated -1 and reported to work in the emulator only. Also I doubt if I could completely abandon the original launcher, as otherwise there is no access anymore to e.g. the USB mass device control to allow new HTML files to be downloaded, the application to be killed and restarted, and so forth.

I'm willing to go for a kludge as well. Maybe a background service could be started that would bring the application to the front again as necessary?


回答1:


Can I achieve this by making my application itself the launcher? How can I then get back to the original launcher upon the home, back, home sequence? It seems that this goes deep into the innards of Android, huh?

Yes. Not too deep into the innards. You can manually start a launcher by specifying the component, note that this may vary from device to device and user to user, if you're just using this privately you could hard code it, but if you release it you must allow the user to specify their real home app.

/* This should come from a preference that let's the user select an activity that can handle the HOME intent */
String packageName = "com.android.launcher";
String packageClass = "com.android.launcher2.Launcher";

Intent home_intent = new Intent(Intent.ACTION_MAIN);
home_intent.addCategory(Intent.CATEGORY_HOME);
home_intent.setComponent(new ComponentName(packageName, packageClass));
home_intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
/* Here you should catch the exception when the launcher has been uninstalled, and let the user save themselves by opening the Market or an app list or something. Users sometimes use root apps to uninstall the system launcher, so your fake launcher is all that is left. Might as well give the poor user a hand. */
startActivity(home_intent);

Detecting home/back/home is a bit awkard because home won't come as a onKeyEvent but instead as a new intent. Simply long-pressing the back button then display a prompt is probably a safe/good approach.




回答2:


The Home button cannot be overriden. You can write an application that responds to the home intent (that's what a launcher does) but that's all.



来源:https://stackoverflow.com/questions/6836869/android-how-to-control-the-home-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!