在android中创建自定义LockScreen

kk1*_*kk1 16 android lockscreen

我正在开发自定义锁屏应用程序在4.0以下但4.0以上正常工作,当我们按下主页按钮应用程序stops.is有任何解决方案这个没有应用程序将停止按下主页按钮直到解锁屏幕.(如去更衣室应用程序)

Mig*_*uel 13

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

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

((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
Run Code Online (Sandbox Code Playgroud)

你应该把这行代码放在你的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你阅读了WindowManager.LayoutParams.TYPE_SYSTEM_ERROR它的文档,它解释了这是一种内部系统错误窗口,它们可以显示在所有内容之上.在多用户系统中,仅在拥有用户的窗口中显示.

所以现在你有一个活动在所有东西之上,但按下HOME按钮将退出活动.

这是视图出现的地方.您可以从布局资源中扩展视图并将其添加到WindowManagera TYPE_SYSTEM_ERROR,因此将在所有内容之上.由于您可以控制何时删除此视图,因此最好的位置是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();
    }
Run Code Online (Sandbox Code Playgroud)

为了避免显示的通知栏,我添加了标志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" />
Run Code Online (Sandbox Code Playgroud)

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

  • @prince这是一件坏事,锁屏应该完全阻止该键以防止显示最近的应用程序. (2认同)

nun*_*123 0

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

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
Run Code Online (Sandbox Code Playgroud)

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