其他分享
首页 > 其他分享> > Android:如何控制主页按钮

Android:如何控制主页按钮

作者:互联网

我们试图向邻居的精神和身体残疾女儿提供一个应用程序,让她使用Android平板电脑作为Talker,即,她按下几个大按钮,设备会生成语音.该应用程序基本上是一个WebView和Javascript中用于执行和控制语音生成的附加对象,以及一些处理方向更改的逻辑. Html文件是针对其特定的谈话项目布局而离线生成的.我们还增加了一些音乐播放和图片查看功能,使设备对她更具吸引力.

问题是主页按钮让她回到Android启动器屏幕的疯狂状态,而在测试设备(Archos 70)上,主页按钮不是物理按钮,而是显示在触摸屏上,这使得它太容易了不小心打了它.

所以我想回到Android启动器只能通过按顺序回家,回家,没有其他动作.

我可以通过使我的应用程序本身成为发射器来实现这一目标吗?然后,我怎样才能回到原来的发射器,回到家,后,回家的序列?这似乎深入到了Android的内部,是吧?

到目前为止,我发现的唯一线索是Overriding Home button for a Car Home replacement app,但这个等级为-1,并且报告仅在模拟器中工作.此外,我怀疑是否可以完全放弃原始启动器,否则将无法再访问原始启动器. USB大规模设备控制允许下载新的HTML文件,要杀死和重新启动的应用程序,等等.

我也愿意去找一个kludge.也许可以启动后台服务,以便在必要时将应用程序重新启动?

解决方法:

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?

是.不太深入
内脏.您可以通过指定组件来手动启动启动器,请注意,这可能因设备和用户而异,如果您只是私下使用它,您可以对其进行硬编码,但如果您发布它,则必须允许用户指定他们真实的家庭应用.

/* 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);

检测home / back / home有点尴尬,因为home不会作为onKeyEvent而是作为新意图.只需长按后退按钮然后显示提示可能是一种安全/好的方法.

标签:android,onkeydown,android-homebutton
来源: https://codeday.me/bug/20190614/1236392.html