公司项目新增一个需求,需要监听手机的系统截屏,上报埋点数据。就借此机会做了一个可以监听android和ios手机系统截屏的插件。本人是android开发,此插件的android部分是自己完成的,ios的部分是由公司ios同事完成的。 插件地址:github.com/fish89757/s…
注意:Android实现截屏监听的方法是,给图库添加一个监听器,监听截屏这个文件夹的变化,所以这里是需要外部存储读取权限的,插件中也提供了没有这个权限时的回调,可以在此回调中处理没有权限的情况。
使用方法(可以使用级联操作符调用,也可以分步调用):
//可以使用级联操作符调用
ScreenShotListenPlugin.instance
..startListen()
..addScreenShotListener((path) {
debugPrint("截屏>>>>>>>>>>$path");
})
..addNoPermissionListener(() {
debugPrint("没有相册权限>>>>>>>>>>");
Permission.storage.request();
});
分步调用
1.打开监听
//打开监听
ScreenShotListenPlugin.instance.startListen();
2.添加截屏事件回调,path为截图的路径(目前只有Android有这个路径)
ScreenShotListenPlugin.instance.addScreenShotListener((path){});
3.Android可以添加权限处理事件回调,可以在此回调中处理没有读取存储权限的情况。
ScreenShotListenPlugin.instance.addNoPermissionListener((){
//可在此处申请权限
});
4.结束监听
ScreenShotListenPlugin.instance.stopListen();
目前回调中图片路径参数,只有android可以拿到,ios暂时还不知道怎么获取这个路径,以后找到方法会更新上去。