标签:
<!-- 系统窗口,显示在最顶层 --> < uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" />
<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>
</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; } }
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); }; }
标签:
原文地址:http://blog.csdn.net/u011370871/article/details/43485075