本文提供一个实现将自定义的界面显示在系统最顶层,达到锁定系统,禁止返回和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; @Override public 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() { @Override public void onClick(View v) { // 关闭锁屏界面 removeViewFromWindow(); } }); wm.addView(lockScreenView, mLayoutParams); }; /** 关闭锁屏界面 */ public void removeViewFromWindow() { if (lockScreenView != null) { wm.removeView(lockScreenView); } } @Override public 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 { @Override protected 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() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // get Binder } @Override public void onServiceDisconnected(ComponentName name) { } }; // 解绑服务 protected void onDestroy() { super.onDestroy(); unbindService(connServiceConnection); }; }
时间: 2024-11-08 10:28:06