自定义锁屏界面,锁定系统,禁止返回和home键关闭界面

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);};}

不义而富且贵,于我如浮云。

自定义锁屏界面,锁定系统,禁止返回和home键关闭界面

相关文章:

你感兴趣的文章:

标签云: