在 android 中创建自定义 LockScreen [关闭]

问题描述 投票:0回答:5

我正在开发自定义锁屏应用程序。它在 4.0 以下但在 4.0 以上时工作正常,当我们按下主页按钮时应用程序停止。是否有任何解决方案,当按下主页按钮直到解锁屏幕时,应用程序将停止。(比如 go locker应用程序)

android lockscreen
5个回答
14
投票

另一种开发锁屏应用程序的方法是使用Views,让我解释一下。

首先,您可以通过禁用

KEYGUARD

在某些设备中“禁用”系统锁定屏幕
((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();

You should put this line of code in your

Service
.

之后你可以在每次屏幕熄灭时启动一个活动:

public class AutoStart extends BroadcastReceiver {

    public void onReceive(Context arg0, Intent arg1) {
        if(arg1.getAction().equals("android.intent.action.SCREEN_OFF")) {
            Intent localIntent = new Intent(arg0, LockScreen.class);
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            localIntent.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
            arg0.startActivity(localIntent);
        }
    }
}

如果您阅读

WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
的文档,它解释说 一种内部系统错误窗口,出现在它们可以出现的所有内容之上。在多用户系统中仅显示在拥有用户的窗口中。

所以现在您在所有内容之上都有一个活动,但是按

HOME
按钮将退出活动。

这里是Views出现的地方。您可以从布局资源中扩充视图并将其作为

WindowManager
添加到
TYPE_SYSTEM_ERROR
,因此将位于所有内容之上。由于您可以控制何时删除此 View,因此最好的位置是在您的
onDestroy
Activity
中,因为按下
HOME
按钮只会暂停您的活动,视图仍然可见。

public WindowManager winManager;
public RelativeLayout wrapperView;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);
        this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
        this.wrapperView = new RelativeLayout(getBaseContext());
        getWindow().setAttributes(localLayoutParams);
        View.inflate(this, R.layout.lock_screen, this.wrapperView);
        this.winManager.addView(this.wrapperView, localLayoutParams);
}

 public void onDestroy()
    {
        this.winManager.removeView(this.wrapperView);
        this.wrapperView.removeAllViews();
        super.onDestroy();
    }

为了避免显示通知栏,我添加了标志

FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN
以消耗所有指针事件。

不要忘记将这两行添加到您的清单中:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

从这里您只需添加锁屏应用程序的逻辑,让用户使用他的智能手机:)


0
投票

自定义启动器基本上是一个应用程序(你可以让它表现得像一个网格,列表,实现你自己的拖放等)然后,你只需要将这些行添加到主要活动的意图过滤器,完成后,安装您的应用程序并按下主页按钮后,您的应用程序将出现在可用主屏幕列表中。

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

我找不到替换锁屏的方法,而像禁用手机锁屏和使用自定义启动器中的活动这样的黑客攻击实际上并不能替换锁屏 ^^


0
投票

我正在三星 Galaxy S4 5.0 上开发,对我有用的只是将

getWindow().setFlags(..)
更改为
getWindow().addFlags(..)


0
投票

我认为首先你应该问问自己是否真的想劫持主页键。有时你可能想要它。但我认为将应用程序放置在 Android 锁屏上,让主页键正常运行并让底层 Android 锁屏负责密码保护设备是在很多情况下你真正想要的(除非你想更改这是默认完成的方式)。
最重要的是,让应用程序显示在 Android 锁定屏幕上非常接近于编写您自己的自定义锁定屏幕。而且显然更容易,因为您不必自己管理密码。更不用说它更安全、更可靠,因为你不会劫持主页键。
我是这样做的,效果很好。您可以在这里查看详细信息:

在 Android 锁屏上显示网站

问题是关于在锁定屏幕上显示网站,因为这是我感兴趣的,但答案更笼统,它适用于任何应用程序。 你可以在这里看到一个在 Google Play 上的应用程序,它是这样写的:

https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime


0
投票

您可以使用以下方法禁用 android 中的 Home 键:

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    super.onAttachedToWindow();
}
© www.soinside.com 2019 - 2024. All rights reserved.