欢迎来到天天文库
浏览记录
ID:40523164
大小:74.50 KB
页数:8页
时间:2019-08-04
《Android截屏事件监听》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Android截屏事件监听1.前言Android系统没有直接对截屏事件监听的接口,也没有广播,只能自己动手来丰衣足食,一般有三种方法。利用FileObserver监听某个目录中资源变化情况利用ContentObserver监听全部资源的变化监听截屏快捷按键由于厂商自定义Android系统的多样性,再加上快捷键的不同以及第三方应用,监听截屏快捷键这事基本不靠谱,可以直接忽略。本文使用的测试手机,一加2(OnePlus2)。2.FileObserver添加权限:2、ission.READ_EXTERNAL_STORAGE"/>代码示例:publicclassScreenshotActivityextendsAppCompatActivity{privatefinalStringTAG="Screenshot";privatestaticfinalStringPATH=Environment.getExternalStorageDirectory()+File.separator+Environment.DIRECTORY_PICTURES+File.separator+"Screenshots"+Fi3、le.separator;protectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_screenshot);mFileObserver=newCustomFileObserver(PATH);}@OverrideprotectedvoidonResume(){super.onResume();mFileObserver.startWatching();4、Log.d(TAG,PATH);}@OverrideprotectedvoidonStop(){super.onStop();mFileObserver.stopWatching();}/***目录监听器*/privateclassCustomFileObserverextendsFileObserver{privateStringmPath;publicCustomFileObserver(Stringpath){super(path);this.mPath=path;}publicCustomFileObserver(Stringpat5、h,intmask){super(path,mask);this.mPath=path;}@OverridepublicvoidonEvent(intevent,Stringpath){Log.d(TAG,path+""+event);//监听到事件,做一些过滤去重处理操作}}}打印的日志:一加2D/Screenshot:Screenshot_2016-12-16-17-49-18.png256D/Screenshot:Screenshot_2016-12-16-17-49-18.png32D/Screenshot:Screenshot_26、016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screensh7、ot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-188、.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-1
2、ission.READ_EXTERNAL_STORAGE"/>代码示例:publicclassScreenshotActivityextendsAppCompatActivity{privatefinalStringTAG="Screenshot";privatestaticfinalStringPATH=Environment.getExternalStorageDirectory()+File.separator+Environment.DIRECTORY_PICTURES+File.separator+"Screenshots"+Fi
3、le.separator;protectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_screenshot);mFileObserver=newCustomFileObserver(PATH);}@OverrideprotectedvoidonResume(){super.onResume();mFileObserver.startWatching();
4、Log.d(TAG,PATH);}@OverrideprotectedvoidonStop(){super.onStop();mFileObserver.stopWatching();}/***目录监听器*/privateclassCustomFileObserverextendsFileObserver{privateStringmPath;publicCustomFileObserver(Stringpath){super(path);this.mPath=path;}publicCustomFileObserver(Stringpat
5、h,intmask){super(path,mask);this.mPath=path;}@OverridepublicvoidonEvent(intevent,Stringpath){Log.d(TAG,path+""+event);//监听到事件,做一些过滤去重处理操作}}}打印的日志:一加2D/Screenshot:Screenshot_2016-12-16-17-49-18.png256D/Screenshot:Screenshot_2016-12-16-17-49-18.png32D/Screenshot:Screenshot_2
6、016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screensh
7、ot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-17-49-18
8、.png2D/Screenshot:Screenshot_2016-12-16-17-49-18.png2D/Screenshot:Screenshot_2016-12-16-1
此文档下载收益归作者所有