Rua*_*uag 17 android homescreen screen-lock android-homebutton
我知道很多次都会问这个问题,但我发现没有一个解决方案可行.我尝试了下面给出的代码......
protected void onPause() {
super.onPause();
Intent intent = new Intent(this,LockActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
它的作用是当Android主屏幕启动时它将当前活动再次带到前面,但是当主屏幕启动时,将活动再次带到前面需要将近3-4秒.
我使用了一些锁屏应用程序,当点击主页按钮时,它们甚至都没有启动主屏幕.我希望实现这样的目标.
我还使用了onUserLeavesHint方法,onKeyDown方法和onKeyDispatch方法,但它们都没有为我工作.
请不要回答或评论,因为无法在Android中禁用主页按钮.对于这样的答案或评论,我建议你在PlayStore上浏览一些锁屏应用程序.我还在github上找到了一个有源代码的工作应用程序.它正在我的手机上工作,应用程序使用disableKeyguard,但是当我在我的应用程序中执行相同操作时它不起作用(不支持disableKeyguard,但我使用@supress警告("弃用")).
来源 - https://github.com/shaobin0604/Android-HomeKey-Locker
//Copy this class
public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
}
}
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
private static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F; // transparent
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
protected final void onCreate(Bundle bundle) {
super.onCreate(bundle);
FrameLayout framelayout = new FrameLayout(getContext());
framelayout.setBackgroundColor(0);
setContentView(framelayout);
}
}
}
//Paste this in your activity
mHomeKeyLocker = new HomeKeyLocker();
mHomeKeyLocker.lock(this);
Run Code Online (Sandbox Code Playgroud)
Pri*_*ais -1
你可以做的是像这样覆盖主页键功能:
\n\n@Override\xc2\xa0public boolean onKeyDown(int keyCode, KeyEvent event)\n{\n if(keyCode == KeyEvent.KEYCODE_HOME)\n {\n //The Code Want to Perform.\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n\n那么您应该能够阻止用户使用此按钮返回主屏幕。\n希望这对您有用。
\n\n编辑:\n覆盖主页按钮的问题是 Google 认为它是一个安全漏洞,因此每次有人找到覆盖它的方法时,Google 都会修补这个“漏洞”。\n\xc2\xa0
\n| 归档时间: |
|
| 查看次数: |
22438 次 |
| 最近记录: |