在 android 中创建自定义锁屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21983462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating custom LockScreen in android
提问by kk1
I am developing custom lockscreen app.its working fine in below 4.0 but above 4.0,when we press home button the app stops.is there any solution for this no apps will stop when pressing home button untill unlocking the screen.(like go locker app)
我正在开发自定义锁屏应用程序。它在 4.0 以下但在 4.0 以上工作正常,当我们按下主页按钮时,应用程序停止。是否有任何解决方案? )
回答by Miguel
Another way to develop a LockScreen App is by using Views, let me explain it.
开发锁屏应用程序的另一种方法是使用Views,让我解释一下。
First of all you can "disable" in some devices the System lock screen by disabling the KEYGUARD
:
首先,您可以通过禁用以下功能在某些设备中“禁用”系统锁定屏幕KEYGUARD
:
((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
You should put this line of code in your Service
.
你应该把这行代码放在你的Service
.
After that you can launch an activity every time the screen goes off:
之后,您可以在每次屏幕熄灭时启动一个活动:
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);
}
}
}
If you read the documentation for WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
it explains that is a type of internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.
如果你阅读了WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
它的文档,它解释说这是一种内部系统错误窗口,出现在他们能做到的一切之上。在多用户系统中仅在拥有用户的窗口上显示。
So now you have an activity on top of everything, but a press in HOME
button will exit the activity.
所以现在你在所有东西之上都有一个活动,但按下HOME
按钮将退出活动。
Here is where the Viewsmake their appearance. You can inflate a view from a layout resource and add it to the WindowManager
as a TYPE_SYSTEM_ERROR
, so will be on top of everything. And since you can control when to remove this View, the best place is in onDestroy
of your Activity
, because pressing the HOME
button will only pause your activity, and the view will still be visible.
这里是视图出现的地方。您可以从布局资源扩充视图并将其WindowManager
作为 a添加到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();
}
To avoid the notification bar of showing I added the flags FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN
to consume all pointer events.
为了避免显示通知栏,我添加了标志FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN
来消耗所有指针事件。
Not forget to add these two lines to your manifest:
不要忘记将这两行添加到您的清单中:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
From here you just need to add the logic of your Lock Screen app to let the user use his smartphone :)
从这里你只需要添加你的锁屏应用程序的逻辑,让用户使用他的智能手机:)
回答by superuserdo
I am developing on a Samsung Galaxy S4 5.0 and what worked for me was simply changing getWindow().setFlags(..)
to getWindow().addFlags(..)
我正在三星 Galaxy S4 5.0 上开发,对我有用的只是更改getWindow().setFlags(..)
为getWindow().addFlags(..)
回答by VSim
I think first of all you should ask yourself if you really want to hiHyman the home key. Sometimes you may want it. But I think placing the app on the Android lock screen, letting the home key act normally and letting the underlying Android lock screen take care of password-protecting the device is what you actually want in a lot of cases (unless you want to change the way this is done by default).
Bottom line, letting an app be displayed on the Android lock screen comes pretty close to writing your own custom lock screen. And is decidedly easier since you don't have to manage passwords yourself. Not to mention it's safer and more reliable since you don't hiHyman the home key.
I did it like this and it works very well. You can see the details here:
我认为首先你应该问问自己你是否真的想劫持home键。有时你可能想要它。但我认为将应用程序放在 Android 锁屏上,让 Home 键正常运行并让底层的 Android 锁屏处理密码保护设备是很多情况下您真正想要的(除非您想更改这是默认完成的方式)。
最重要的是,让应用程序显示在 Android 锁定屏幕上非常接近于编写自己的自定义锁定屏幕。而且绝对更容易,因为您不必自己管理密码。更不用说它更安全,更可靠,因为您不会劫持主页键。
我是这样做的,而且效果很好。您可以在此处查看详细信息:
show web site on Android lock screen
The question is about displaying a website on the lock screen, since that's what I was interested in, but the answer is more general, it works with any app. You can see here an app that's on Google Play and has been written like this:
问题是关于在锁定屏幕上显示网站,因为这是我感兴趣的,但答案更笼统,它适用于任何应用程序。您可以在这里看到 Google Play 上的一个应用程序,它是这样编写的:
https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime
https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime
回答by nunoh123
A custom launcher is basically an app (you can make it behave like a grid, list, implement your own drag and drop etc) then, you only need to add these lines to the intent filter of the main activity, with this done, after you install your app and press the home button your app will appear in the list of available homescreens.
自定义启动器基本上是一个应用程序(您可以使其表现得像一个网格、列表、实现您自己的拖放等)然后,您只需要将这些行添加到主要活动的意图过滤器中,完成后您安装您的应用程序并按下主页按钮,您的应用程序将出现在可用主屏幕列表中。
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
What i cant find is a way to replace the lock screen, and hacks like disabling the lock screen on the phone and using an activity in a custom launcher isn't actually replacing the lockscreen ^^
我找不到的是一种替换锁屏的方法,而像禁用手机上的锁屏和在自定义启动器中使用活动这样的黑客实际上并没有替换锁屏 ^^
回答by Snehal Masne
You can use the below method to disable the Home key in android :
您可以使用以下方法禁用 android 中的 Home 键:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}