提交 3ed223b2 编写于 作者: 杜庆泉's avatar 杜庆泉

截屏监听插件 增加状态码,减少权限请求接口。与微信保持一致

上级 a05bfe18
......@@ -3,7 +3,6 @@
<page-head title="监听系统截屏" accordion></page-head>
<uni-list>
<uni-list-item @tap="testScreenShotPremission" title="准备截屏监听权限(仅Android)" class="itemButton" :clickable="true"/>
<uni-list-item @tap="testScreenShotListen" title="监听截屏事件" class="itemButton" :clickable="true"/>
<uni-list-item @tap="testScreenShotOff" title="关闭截屏监听" class="itemButton" :clickable="true"/>
</uni-list>
......@@ -15,7 +14,6 @@
<script>
import {
requestPremission,
onUserCaptureScreen,
offUserCaptureScreen
} from "@/uni_modules/uts-screenshot-listener";
......@@ -29,21 +27,19 @@
methods: {
testScreenShotPremission() {
if (uni.getSystemInfoSync().platform == "ios") {
uni.showToast({
icon:"none",
title:'iOS 平台无需请求权限'
})
return;
}
// 请求写入储存的权限
requestPremission();
},
testScreenShotListen() {
var that = this;
onUserCaptureScreen(function(res) {
console.log(res);
if(res.code == -1){
// 启动失败
return ;
}else if(res.code == 0){
uni.showToast({
icon:"none",
title:'截屏监听已开启'
})
}else {
uni.showToast({
icon:"none",
title:'捕获截屏事件'
......@@ -51,12 +47,9 @@
if (uni.getSystemInfoSync().platform == "android") {
that.screenImage = res.image
}
}
});
// 提示已经开始监听,注意观察
uni.showToast({
icon:"none",
title:'截屏监听已开启'
})
},
testScreenShotOff() {
var that = this;
......
......@@ -65,6 +65,7 @@ class ScreenFileObserver extends FileObserver {
lastFileObserverTime = System.currentTimeMillis()
let ret = {
code:1,
image:newPath
}
......@@ -79,30 +80,24 @@ class ScreenFileObserver extends FileObserver {
/**
* 请求权限
*/
export function requestPremission() {
// 注册一个请求回调
// 发起权限申请
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
}
// 请求权限
return { name: "requestPremission" };
}
/**
* 开启截图监听
*/
export function onUserCaptureScreen(callback: (res:UTSJSONObject) => void) {
// 检查相关权限是否已经具备
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 不具备权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
// 因权限缺失导致监听失败
let ret = {
code:-1
}
callback(ret);
return ;
}
imageChange = callback;
......@@ -124,6 +119,12 @@ export function onUserCaptureScreen(callback: (res:UTSJSONObject) => void) {
screenOB = new ScreenFileObserver(directory_screenshot.path)
screenOB!.startWatching()
// 监听成功
let ret = {
code:0
}
callback(ret);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册