android - 在android中创建自定义LockScreen

标签 android lockscreen

我正在开发自定义锁屏应用程序。它在 4.0 以下但高于 4.0 时工作正常,当我们按下主页按钮时,应用程序停止。有没有任何解决方案可以解决这个问题,当按下主页按钮直到解锁屏幕时,没有应用程序会停止。(比如去储物柜应用程序)

最佳答案

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

首先,您可以通过禁用 KEYGUARD 在某些设备中“禁用”系统锁定屏幕:

((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();

你应该把这行代码放在你的Service中。

之后,您可以在每次屏幕关闭时启动一个 Activity :

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 的文档,它会说明 这是一种内部系统错误窗口,出现在所有可能出现的位置之上。在多用户系统中,仅在拥有用户的窗口上显示。

所以现在你有一个最重要的 Activity ,但是按下 HOME 按钮将退出 Activity 。

View 在这里出现。您可以从布局资源中扩展 View 并将其作为 TYPE_SYSTEM_ERROR 添加到 WindowManager,因此将位于所有内容之上。而且由于您可以控制何时删除此 View,因此最好的位置是在 ActivityonDestroy 中,因为按下 HOME 按钮只会暂停您的 Activity ,并且 View 仍然可见。

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 消耗所有指针事件。

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

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

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

关于android - 在android中创建自定义LockScreen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21983462/

相关文章:

javascript - 如何防止机器人程序和垃圾 API 请求?

java - 布局错误无法弄清楚

java - 在我的应用程序上创建 "lock screen"

android - 全屏透明 Activity (无标题和状态栏)不起作用....为什么?

ios - 无法从锁屏中删除本地通知

android - 如何在滚动时找到显示在 RecyclerView 顶部的项目

java - Android 上翻译器对象的抽象类或接口(interface)

java - 如何使用不同的延迟参数持续更新textView

android - 当我在前台使用 flutter 应用程序解锁手机时,它会绕过身份验证

ios - 当iPhone像iPod App一样被锁定时,继续播放音频