首先,须要启动一个后台服务,用于注册一个BroadcastReceiverandroid

这个BroadcastReceiver用于监听Intent.ACTION_SCREEN_OFFapp

这样在发生这个事件Intent.ACTION_SCREEN_OFF 也就是用户锁屏或者屏幕上锁时触发。ide

Service中注册BroadcastReceiver代码函数

package com.pingbao;

import android.app.KeyguardManager;

import android.app.Service;

import android.app.KeyguardManager.KeyguardLock;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.IBinder;

import android.util.Log;

public class ZyScreenService extends Service {

KeyguardManager mKeyguardManager = null;

private KeyguardLock mKeyguardLock = null;

@Override

public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate()

{

// TODO Auto-generated method stub

super.onCreate();

}

@Override

public void onStart(Intent intent, int startId)

{

// TODO Auto-generated method stub

Log.i("in Service","in Service");

mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);

mKeyguardLock = mKeyguardManager.newKeyguardLock("");

mKeyguardLock.disableKeyguard();

Log.i("in Service1","in Service1");

BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

try {

Intent i = new Intent();

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

i.setClass(context, ZyScreenSaver.class);

context.startActivity(i);

// finish();

Log.i("BroadcastReceiver","BroadcastReceiver");

} catch (Exception e) {

Log.i("Output:", e.toString());

}

}

};

registerReceiver(mMasterResetReciever, new IntentFilter(

Intent.ACTION_SCREEN_OFF));

}

} ui

而后在咱们启动的ZyScreenSaver这个屏保界面Activity里写上this

package com.pingbao;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.DialogInterface.OnClickListener;

import android.os.Bundle;

import android.util.Log;

import android.view.KeyEvent;

import android.view.Window;

import android.view.WindowManager;

import android.widget.ArrayAdapter;

public class ZyScreenSaver extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);

Log.i("MyScreenSaver", "MyScreenSaver");

}

public boolean onKeyDown(int keyCode, KeyEvent event) {

super.onKeyDown(keyCode, event);

if (keyCode == KeyEvent.KEYCODE_BACK) {

Log.i("4", "4");

showDialog(1);

}

// finish();

return true;

}

protected Dialog onCreateDialog(int id) {

switch (id) {

case 1:

return new AlertDialog.Builder(ZyScreenSaver.this).setIcon(

R.drawable.icon).setTitle("退出屏保").setMessage(

"是否退出屏保").setPositiveButton("肯定",

new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

finish();

}

})

.setNegativeButton("取消",

new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

removeDialog(1);

}

})

.create();

}

return null;

}

}spa

而后在咱们的主界面里写上启动服务就能够了.net

package com.pingbao;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.Window;

import android.view.WindowManager;

public class Zypingbao extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Log.i("pingbao","pingbao");

Intent mService=new Intent(Zypingbao.this,ZyScreenService.class);//启动服务

mService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startService(mService);

}

} code

在Manifest里贴权限blog

源码下载

若是实在要屏蔽home键 还有一种办法 就是

public void onAttachedToWindow() {

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

super.onAttachedToWindow();

}

这样在

public boolean onKeyDown(int keyCode, KeyEvent event)

函数里就能监听到home了

本博客版权归CSDN博主曾阳全部

转载请注明

android中屏幕保护的实现的,Android 屏幕保护程序制做及源码相关推荐

  1. android中编译和使用luajit开发应用,Android 嵌入 LuaJIT 的曲折道路

    相关链接:Windows 下编译 LuaJIT 懒人与伸手党可以直接看最底部. 为什么使用 LuaJIT Lua 官方版的编译嵌入相对简单,但是为什么要用 LuaJIT 呢?我所了解到的优势有: 更高 ...

  2. Android中layout过程详解 (结合Android 4.0.4 最新源码)

    上一篇文章Android中mesure过程详解 (结合Android 4.0.4 最新源码)介绍了View树的measure过程,相对与measure过程,本文介绍的layout过程要简单多了,正如l ...

  3. Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

    Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题 这是API ...

  4. android中调用fft函数,J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data)...

    J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data) 我一直在玩这个游戏已经有一段时间了,我无法弄清楚我在这里要做的事情. ...

  5. Android中mesure过程详解 (结合Android 4.0.4 最新源码)

    如何遍历并绘制View树?之前的文章Android中invalidate() 函数详解(结合Android 4.0.4 最新源码)中提到invalidate()最后会发起一个View树遍历的请求,并通 ...

  6. android 开发零起步学习笔记(二十二):ANDROID应用ACTIVITY、DIALOG、POPWINDOW、TOAST窗口添加机制及源码分析(一)

    原文:http://www.cnblogs.com/shanzei/p/4654817.html 第一部分: ANDROID应用ACTIVITY.DIALOG.POPWINDOW.TOAST窗口添加机 ...

  7. Android自定义View之(一)View绘制流程详解——向源码要答案

    前言 View作为整个app的颜值担当,在Android体系中占有重要的地位.深入理解Android View的绘制流程,对正确使用View来构建赏心悦目的外观,以及用自定义View来设计理想中的酷炫 ...

  8. Android 签到打卡日历,自定义日期可带图标(附源码)

    前言 公司项目需要做一个签到送积分的系统,要求app实现这种签到功能.先在网上找了一些资料,有些用自定义View画图实现,其实对于普通码农对绘图会没耐心看各自算法.所以自己动手撸一个通用型日历,使用V ...

  9. android做拨号程序代码,Android开发手机拨号程序实现实例源码介绍

    Android开发手机拨号程序实现实例源码介绍,在上一篇文章中,我们实现了第一个程序:helloWorld,并成功测试完成.还给大家介绍了Android项目结构和说明.现在写一个手机拨号程序: 首先, ...

  10. java实现高德地图app,Android 高德地图入门demo,最新高德地图实现方法,附源码及apk...

    [实例简介] Android 高德地图入门demo,最新高德地图实现方法,附源码及apk [实例截图] [核心代码] GaoDeDemo ├── GaoDeDemo │   ├── app │   │ ...

最新文章

  1. Spring_Hibernate整合准备
  2. 618 京东到家-小程序也狂欢
  3. 怎样把本软件是否注册的标志加在程序里?(注:不想加在数据库里)
  4. iis php报错无法屏蔽,php屏蔽错误消息
  5. 数据结构和算法(01)--- 算法复杂度
  6. Ubuntu学习小结(一) 基础知识,系统安装,软件安装,解压缩
  7. c语言程序综合实习学生成绩,C语言程序设计综合实习报告
  8. 院士建议:多关注千千万万没有任何“帽子”的青年科技工作者
  9. Android风格与主题
  10. java序列化 反序列化_Java序列化– Java序列化
  11. RDLC之自定義數據集二
  12. Posterino for Mac(图片拼贴编辑器)
  13. 防热服的设计数学建模_数学建模之高温作业专用服装设计.pdf
  14. unity模型制作(四):绘制一个凹多边形
  15. 校验一,两位小数0-999999.99,填写其他内容提示“成绩填写有误”
  16. glassfish配置错误问题
  17. 实习每日总结_20161214
  18. 没那么简单,没那么困难
  19. python VS matlab: reshape/max/matrix index等方法比较
  20. 微分算法 非侵入式负荷识别_非侵入式负荷监测的识别方法和关键技术

热门文章

  1. 一个特别好用的免费json数据API接口--特别推荐
  2. 基于java+springboot+mysql的校园二手交易平台
  3. 整车车辆七自由度垂向动力学模型
  4. linux下caffe安装过程原理,caffe安装过程详解linux版本
  5. jQuery-表单验证使用方法
  6. html页面怎么记住密码,怎么让网页记住密码?让网页记住密码的方法
  7. 昆仑通态屏幕制作(连载5)---基础篇(串口接收,文本与灯显示)
  8. 手把手BC26模组OpenCPU开发之旅-1.简介
  9. Python生成图文并茂的PDF报告
  10. python傅里叶谐波分析_一种利用快速三角形式傅里叶变换的信号谐波分析方法