自定义锁屏界面,锁定系统,禁止返回和home键关闭界面
来源:互联网 发布:淘宝旺铺css 编辑:程序博客网 时间:2024/06/01 10:21
本文提供一个实现将自定义的界面显示在系统最顶层,达到锁定系统,禁止返回和home键关闭界面的解决方案。
在手机黑屏状态,也能够弹出解锁界面
1添加权限
<!-- 系统窗口,显示在最顶层 --> < uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" />
2创建界面布局文件lock_screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ffffff" android:gravity="center" android:text="屏幕已锁定" /> <Button android:id="@+id/btn" android:layout_marginTop="250dp" android:textColor="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="关闭界面" /></LinearLayout>
3创建服务AppService
Don't go for activity, because android will not show lock screen behind your activity for security reason, so use service instead of Activity.
不要在activity中显示,由于安全原因,安卓系统不会在锁屏时候显示自定义的窗口,所以使用服务代替activity
WindowManager 这个类能够实现系统级别的窗口。
首先设置布局界面的属性
关键是设置TYPE_SYSTEM_ERROR
</pre><pre name="code" class="java">wm = (WindowManager) getSystemService(WINDOW_SERVICE);// 设置lockScreenView视图的属性// 属性TYPE_SYSTEM_ERROR:出现在任何界面的前面mLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, PixelFormat.RGBA_8888);LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);AppService.java完整代码
/* *@author Dawin,2015-2-4 * */package com.example.superlockscreen;import com.example.superlockscreen.R;import android.app.Service;import android.content.Context;import android.content.Intent;import android.graphics.PixelFormat;import android.os.IBinder;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Button;/** * 实现界面锁定。禁止手机的任何按键操作 * @author Dawin * *Don't go for activity, *because android will not show lock screen behind your activity for security reason, *so use service instead of Activity. */public class AppService extends Service{private WindowManager.LayoutParams mLayoutParams;private WindowManager wm;private View lockScreenView;private Button btn;@Overridepublic void onCreate(){super.onCreate();wm = (WindowManager) getSystemService(WINDOW_SERVICE);// 设置lockScreenView视图的属性// 属性TYPE_SYSTEM_ERROR:出现在任何界面的前面mLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, PixelFormat.RGBA_8888);LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);//绑定自定义界面lockScreenView = mInflater.inflate(R.layout.lock_screen, null);//获取界面的按钮btn = (Button) lockScreenView.findViewById(R.id.btn);btn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){// 关闭锁屏界面removeViewFromWindow();}});wm.addView(lockScreenView, mLayoutParams);};/** 关闭锁屏界面 */public void removeViewFromWindow(){if (lockScreenView != null){wm.removeView(lockScreenView);}}@Overridepublic IBinder onBind(Intent intent){return null;}}
4最后在Activity中绑定服务
package com.example.superlockscreen;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// 绑定服务Intent service = new Intent(this, AppService.class);bindService(service, connServiceConnection, BIND_AUTO_CREATE);}private ServiceConnection connServiceConnection = new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service){// get Binder}@Overridepublic void onServiceDisconnected(ComponentName name){}};// 解绑服务protected void onDestroy(){super.onDestroy();unbindService(connServiceConnection);};}
1 0
- 自定义锁屏界面,锁定系统,禁止返回和home键关闭界面
- android 返回HOME界面
- android返回HOME界面
- Android返回HOME界面
- 按下返回金钢键后像Home键一样切换到手机主界面,程序不关闭
- 疑难杂症之——Android点击Home键无法返回主界面,并在松开时闪现锁屏界面
- android解锁界面,插上USB弹出打开USB存储设置界面,按HOME键/返回键直接回到launcher,而非滑动锁屏界面
- Android中PopupWindow点击窗口之外和返回键消失,界面锁定的实现
- Android中PopupWindow点击窗口之外和返回键消失,界面锁定的实现。
- 游戏过程中按Home键后返回游戏界面
- android按home键不能回到自定义主界面
- android按home键不能回到自定义主界面 .
- 按后退键直接回到系统Home界面
- 系统界面自动锁定功能实现原理
- Qt之自定义界面添加最小化、最大化和关闭按钮
- Qt之自定义界面添加最小化、最大化和关闭按钮
- cocos2d-x关于home键黑屏和游戏loading界面
- cocos2d-x 关于home键黑屏和游戏loading界面
- linux添加usb设备
- codeforces 509F Progress Monitoring (区间dp)
- eclipse快捷键总结
- IAP支付
- request.getRemoteAddr()获取IP地址的一些问题
- 自定义锁屏界面,锁定系统,禁止返回和home键关闭界面
- TCP的SEQ和ACK的生成
- pb中怎么获取数据窗口的列名
- MAC软件下载/ SVN_CoreStone/
- 大熊君学习html5系列之------WebStorage(客户端轻量级存储方案)
- PHP 常用字符串处理函数
- AndroidFramework之Volley详细讲解加载网络图片(二)
- Python 好文收集
- vim的一个较全的介绍