当前位置: 代码迷 >> Android >> Android银幕保护KeyguardManager相关内容学习
  详细解决方案

Android银幕保护KeyguardManager相关内容学习

热度:97   发布时间:2016-04-28 07:21:41.0
Android屏幕保护KeyguardManager相关内容学习

?前阵子收到客户要求,要做一款安卓手机和平板上使用的屏保,其实蛮奇怪的,电脑用屏保倒是见得多了,可是手机不使用的时候关掉屏幕不就OK了吗?话说现在的智能手机电池都不耐用的可怜,还装屏保岂不是很费电。原来客户是用于放在营业厅(手机相关),通过手机或者平板来使用相关设备投射到电子屏幕上展示广告的用途,24小时不断电,只是展示用,故电量不作考虑。要求在服务端上传欲展示的图片,PDA上可以进行获取更新图片,只要不断滚动他们的广告就可以了。起初并不是我来做的,同事已经都写的差不多了,他突然有别的项目很急,留给我来做,也好,以前没做过,顺便了解一下android屏保相关的知识,写下来做积累。

-------------------------------------------------------------------------------------------------------

????? 首先接触到了KeyguardManager,用来对系统的屏保进行屏蔽

public class?KeyguardManager?extends Object?

Class that can be used to lock and unlock the keyboard. Get an instance of this class by calling Context.getSystemService(java.lang.String) with argument Context.KEYGUARD_SERVICE. The actual class to control the keyboard locking is KeyguardManager.KeyguardLock.

一个用于锁屏和解锁的类,通过调用Context.getSystemService(Context.KEYGUARD_SERVICE)来获取实例。实际上用于操控锁屏的是KeyguardManager.KeyguardLock类

KeyguardManager?两个内部类分别是:

(1)KeyguardManager.KeyguardLock(l两个函数)???

记得加权限

<uses-permission???android:name="android.permission.DISABLE_KEYGUARD"/>

???????? disableKeyguard()函数来解除锁屏

???????? reenableKeyguard()反解除锁屏.reenableKeyguard()反解除锁屏的意思是:如果在调用disableKeyguard()函数之前是锁屏的,那么就进行锁屏,否则不进行任何操作。当然如果之前没调用disableKeyguard()函数,也不进行任何操作。

(2)KeyguardManager.OnKeyguardExitResult(boolean success)? :返回true表示exitKeyguardSecurely()函数执行成功,否则表示失败<具体自己没用到,是个做判断和debug用的吧估计>

?

-----------------------------------------------------------------------------------------------------

????? 而后用到的是开启和关闭屏幕唤醒的内容,PowerManager和WakeLock这部分之前用到过,不在详述,这次把这部分单独写成一个工具类,简化代码

[java]?view plaincopy
?
  1. package?com.eyu.screen.util;??
  2. ??
  3. import?android.content.Context;??
  4. import?android.os.PowerManager;??
  5. import?android.os.PowerManager.WakeLock;??
  6. ??
  7. public?class?PowerManagerWakeLock?{??
  8. ??
  9. ????private?static?WakeLock?wakeLock;??
  10. ??????
  11. ????/**开启?保持屏幕唤醒*/??
  12. ????public?static?void?acquire(Context?context)?{??
  13. ????????PowerManager?pm?=?(PowerManager)?context.getSystemService(Context.POWER_SERVICE);??
  14. ????????wakeLock?=?pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,?"PowerManagerWakeLock");??
  15. ????????wakeLock.acquire();??
  16. ????}??
  17. ??????
  18. ????/**关闭?保持屏幕唤醒*/??
  19. ????public?static?void?release()?{?????????
  20. ????????if?(wakeLock?!=?null)?{??
  21. ????????????wakeLock.release();??
  22. ????????????wakeLock?=?null;??
  23. ????????}??
  24. ????}??
  25. }??


-------------------------------------------------------------------------------------------------------------------------------------------

其实最主要的应该就是上面的内容,后续就是编写Service和发送广播的操作,并实现和服务端的通信。

?

做完之后抛开服务端和下载更新的内容,自己做了一个单机用的留底,基本思路和操作与公司项目的一致,只是图片需要手动添加而已,使用后会一直监听屏幕的状态,一旦屏幕灭掉会立刻唤醒,使用viewpager和定时器来控制图片自动的翻页,想彻底停掉就需要断掉后台的service,下面是小程序的截图,启动后会先做判断,如果sd卡指定目录没有图片的话,则加载资源文件,有图片加载SD卡中的。

????????????????????????????????????????????????

????????????????????????????????????????????????? ????

?

Service的代码:

[java]?view plaincopy
?
  1. package?com.eyu.screen.UI;??
  2. ??
  3. import?com.eyu.screen.util.PowerManagerWakeLock;??
  4. import?android.app.KeyguardManager;??
  5. import?android.app.Service;??
  6. import?android.content.BroadcastReceiver;??
  7. import?android.content.Context;??
  8. import?android.content.Intent;??
  9. import?android.content.IntentFilter;??
  10. import?android.os.IBinder;??
  11. ??
  12. public?class?ScreenSaverS?extends?Service?{??
  13. ??
  14. ????@Override??
  15. ????public?IBinder?onBind(Intent?intent)?{??
  16. ????????//?TODO?Auto-generated?method?stub??
  17. ????????return?null;??
  18. ????}??
  19. ??
  20. ????public?void?onCreate()?{??
  21. ????????//?屏蔽系统的屏保??
  22. ????????KeyguardManager?manager?=?(KeyguardManager)?getSystemService(Context.KEYGUARD_SERVICE);??
  23. ????????KeyguardManager.KeyguardLock?lock?=?manager??
  24. ????????????????.newKeyguardLock("KeyguardLock");??
  25. ????????lock.disableKeyguard();??
  26. ??
  27. ????????//?注册一个监听屏幕开启和关闭的广播??
  28. ????????IntentFilter?filter?=?new?IntentFilter();??
  29. ????????filter.addAction(Intent.ACTION_SCREEN_ON);??
  30. ????????filter.addAction(Intent.ACTION_SCREEN_OFF);??
  31. ????????registerReceiver(screenReceiver,?filter);??
  32. ????}??
  33. ??
  34. ????BroadcastReceiver?screenReceiver?=?new?BroadcastReceiver()?{??
  35. ??
  36. ????????@Override??
  37. ????????public?void?onReceive(Context?context,?Intent?intent)?{??
  38. ????????????//?TODO?Auto-generated?method?stub??
  39. ????????????String?action?=?intent.getAction();??
  40. ????????????if?(action.equals(Intent.ACTION_SCREEN_ON))?{??
  41. ??
  42. ????????????}?else?if?(action.equals(Intent.ACTION_SCREEN_OFF))?{//如果接受到关闭屏幕的广播??
  43. ????????????????if?(!ScreenSaverShowA.isShow)?{??
  44. ????????????????????//开启屏幕唤醒,常亮??
  45. ????????????????????PowerManagerWakeLock.acquire(ScreenSaverS.this);??
  46. ????????????????}??
  47. ????????????????PowerManagerWakeLock.acquire(ScreenSaverS.this);??
  48. ????????????????Intent?intent2?=?new?Intent(ScreenSaverS.this,??
  49. ????????????????????????ScreenSaverShowA.class);??
  50. ????????????????intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);??
  51. ????????????????startActivity(intent2);??
  52. ????????????????PowerManagerWakeLock.release();??
  53. ????????????}??
  54. ????????}??
  55. ????};??
  56. ??
  57. ????public?void?onDestroy()?{??
  58. ????????PowerManagerWakeLock.release();??
  59. ????????unregisterReceiver(screenReceiver);??
  60. ??
  61. ????};??
  62. ??
  63. }??

?

屏保展示页面代码:

[java]?view plaincopy
?
  1. <span?style="font-size:12px;">package?com.eyu.screen.UI;??
  2. ??
  3. import?java.io.File;??
  4. import?java.util.Timer;??
  5. import?java.util.TimerTask;??
  6. ??
  7. import?com.eyu.screen.R;??
  8. import?com.eyu.screen.adapter.ScreenSaverShowAdapter;??
  9. import?com.eyu.screen.util.FolderUtil;??
  10. ??
  11. import?android.annotation.SuppressLint;??
  12. import?android.app.Activity;??
  13. import?android.graphics.Bitmap;??
  14. import?android.graphics.BitmapFactory;??
  15. import?android.graphics.drawable.Drawable;??
  16. import?android.os.Bundle;??
  17. import?android.os.Handler;??
  18. import?android.os.Message;??
  19. import?android.support.v4.view.ViewPager;??
  20. import?android.util.Log;??
  21. import?android.view.View;??
  22. import?android.view.View.OnClickListener;??
  23. ??
  24. @SuppressLint("HandlerLeak")??
  25. public?class?ScreenSaverShowA?extends?Activity?{??
  26. ????public?static?boolean?isShow?=?false;??
  27. ????private?ViewPager?viewPager;??
  28. ????private?Bitmap[]?bmps?=?null;??
  29. ????private?Drawable[]?drawables?=?new?Drawable[5];??
  30. ??
  31. ????@Override??
  32. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  33. ????????//?TODO?Auto-generated?method?stub??
  34. ????????super.onCreate(savedInstanceState);??
  35. ????????setContentView(R.layout.screenshow);??
  36. ????????isShow?=?true;??
  37. ????????initControl();??
  38. ????????autoSwitch();??
  39. ????}??
  40. ??
  41. ????@Override??
  42. ????protected?void?onDestroy()?{??
  43. ????????//?TODO?Auto-generated?method?stub??
  44. ??
  45. ????????isShow?=?false;??
  46. ????????//将bitmap回收,尽量避免OOM??
  47. ????????if?(bmps?!=?null)?{??
  48. ????????????for?(int?i?=?0;?i?<?bmps.length;?i++)?{??
  49. ????????????????bmps[i].recycle();??
  50. ????????????}??
  51. ????????}?else?{??
  52. ????????????return;??
  53. ????????}??
  54. ????????super.onDestroy();??
  55. ??
  56. ????}??
  57. ??
  58. ????private?void?initControl()?{??
  59. ????????findViewById(R.id.btn_jiesuo).setOnClickListener(new?OnClick());??
  60. ????????viewPager?=?(ViewPager)?findViewById(R.id.viewpager);??
  61. ????????File?f?=?new?File(FolderUtil.getSaveFolder());??
  62. ????????File[]?files?=?f.listFiles();//?得到所有子目录??
  63. ????????bmps?=?new?Bitmap[files.length];??
  64. ??????????
  65. ????????//如果文件夹为空,则从资源文件加载图片??
  66. ????????if?(files.length?==?0)?{??
  67. ????????????drawables[0]?=?getResources().getDrawable(R.drawable.bg_01);??
  68. ????????????drawables[1]?=?getResources().getDrawable(R.drawable.bg_02);??
  69. ????????????drawables[2]?=?getResources().getDrawable(R.drawable.bg_03);??
  70. ????????????drawables[3]?=?getResources().getDrawable(R.drawable.bg_04);??
  71. ????????????drawables[4]?=?getResources().getDrawable(R.drawable.bg_05);??
  72. ????????????ScreenSaverShowAdapter?adapter?=?new?ScreenSaverShowAdapter(??
  73. ????????????????????ScreenSaverShowA.this,?drawables);??
  74. ????????????viewPager.setAdapter(adapter);??
  75. ????????}?else?{??
  76. ????????????//文件夹不为空则循环遍历加载sd卡指定目录中图片??
  77. ????????????for?(int?i?=?0;?i?<?files.length;?i++)?{??
  78. ????????????????String?path?=?files[i].getAbsolutePath();??
  79. ????????????????bmps[i]?=?BitmapFactory.decodeFile(path);??
  80. ????????????????Log.d("PDA",?"====H==="?+?path);??
  81. ??
  82. ????????????}??
  83. ????????????ScreenSaverShowAdapter?adapter?=?new?ScreenSaverShowAdapter(??
  84. ????????????????????ScreenSaverShowA.this,?bmps);??
  85. ????????????viewPager.setAdapter(adapter);??
  86. ??
  87. ????????}??
  88. ??
  89. ????}??
  90. ??
  91. ????/**?图片定时自动切换?*/??
  92. ????private?void?autoSwitch()?{??
  93. ????????int?interval?=?3000;??
  94. ????????Timer?timer?=?new?Timer();??
  95. ????????TimerTask?task?=?new?TimerTask()?{??
  96. ??
  97. ????????????@Override??
  98. ????????????public?void?run()?{??
  99. ????????????????//?TODO?Auto-generated?method?stub??
  100. ????????????????Message?message?=?new?Message();??
  101. ????????????????handler.sendMessage(message);??
  102. ????????????}??
  103. ????????};??
  104. ????????timer.schedule(task,?interval,?interval);??
  105. ????}??
  106. ??
  107. ????Handler?handler?=?new?Handler()?{??
  108. ??
  109. ????????@Override??
  110. ????????public?void?handleMessage(Message?msg)?{??
  111. ????????????int?currentPage?=?viewPager.getCurrentItem();??
  112. ????????????int?tempNum?=?bmps.length?==?0???drawables.length?:?bmps.length;??
  113. ????????????int?nextPage?=?(currentPage?+?1)?%?tempNum;??
  114. ????????????viewPager.setCurrentItem(nextPage);??
  115. ????????????super.handleMessage(msg);??
  116. ????????}??
  117. ??
  118. ????};??
  119. ??
  120. ????class?OnClick?implements?OnClickListener?{??
  121. ??
  122. ????????@Override??
  123. ????????public?void?onClick(View?v)?{??
  124. ????????????//?TODO?Auto-generated?method?stub??
  125. ????????????ScreenSaverShowA.this.finish();??
  126. ????????}??
  127. ??
  128. ????}??
  129. }??
  130. </span>??



?

总结和注意:

1.系统屏保的屏蔽方法以及屏幕唤醒的知识参考API就可以了,比较简单的新知识

2.如果很多张图片来加载的话,很可能出现令人最头疼的OOM,由于屏保就需求全屏显示,所以图片缩放的方式不合适,没想到好办法。不过为了避免反复解锁过程中出现OOM,在onDestroy()方法中对图片进行了recycle。

?

3.在模拟器上运行没有发现问题,但可能在不同的机型上会出现问题,比如有些手机将安卓的源码改掉了,甚至不允许使用屏蔽系统屏保的方法。还是原生态的android系统最好了,都瞎改什么呢。

?

?

转自:http://blog.csdn.net/eyu8874521/article/details/8477724

  相关解决方案