Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
270fe69f
U
uni-api
项目概览
DCloud
/
uni-api
通知
671
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
270fe69f
编写于
10月 21, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
截屏监听插件迁移到 ext api模式
上级
29fd7c77
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
378 addition
and
2 deletion
+378
-2
.hbuilderx/launch.json
.hbuilderx/launch.json
+16
-0
manifest.json
manifest.json
+1
-1
pages/index/index.vue
pages/index/index.vue
+70
-1
uni_modules/uni-screen/changelog.md
uni_modules/uni-screen/changelog.md
+0
-0
uni_modules/uni-screen/package.json
uni_modules/uni-screen/package.json
+87
-0
uni_modules/uni-screen/readme.md
uni_modules/uni-screen/readme.md
+5
-0
uni_modules/uni-screen/utssdk/app-android/index.uts
uni_modules/uni-screen/utssdk/app-android/index.uts
+150
-0
uni_modules/uni-screen/utssdk/app-ios/index.uts
uni_modules/uni-screen/utssdk/app-ios/index.uts
+49
-0
未找到文件。
.hbuilderx/launch.json
0 → 100644
浏览文件 @
270fe69f
{
//
launch.json
配置了启动调试时相关设置,configurations下节点名称可为
app-plus/h
5
/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp
-360
/
//
launchtype项可配置值为local或remote
,
local代表前端连本地云函数,remote代表前端连云端云函数
"version"
:
"0.0"
,
"configurations"
:
[{
"app-plus"
:
{
"launchtype"
:
"local"
},
"default"
:
{
"launchtype"
:
"local"
},
"type"
:
"uniCloud"
}
]
}
manifest.json
浏览文件 @
270fe69f
...
...
@@ -68,5 +68,5 @@
"uniStatistics"
:
{
"enable"
:
false
},
"vueVersion"
:
"
2
"
"vueVersion"
:
"
3
"
}
pages/index/index.vue
浏览文件 @
270fe69f
...
...
@@ -4,10 +4,17 @@
<view
class=
"text-area"
>
<text
class=
"title"
>
{{
title
}}
</text>
</view>
<button
@
tap=
"testScreenShotListen"
>
开启截屏监听
</button>
<button
@
tap=
"testScreenShotOff"
>
关闭截屏监听
</button>
<button
@
tap=
"testGetBatteryInfo"
>
获取电池电量
</button>
</view>
</
template
>
<
script
>
import
getBatteryInfo
from
"
@/uni_modules/uni-getbatteryinfo
"
;
export
default
{
data
()
{
return
{
...
...
@@ -18,7 +25,69 @@
},
methods
:
{
testScreenShotListen
()
{
var
that
=
this
;
uni
.
onUserCaptureScreen
(
function
(
res
)
{
console
.
log
(
res
);
if
(
uni
.
getSystemInfoSync
().
platform
==
"
android
"
)
{
// 除android 之外的平台,不需要判断返回状态码
if
(
res
.
code
==
-
1
){
// 启动失败
return
;
}
else
if
(
res
.
code
==
0
){
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
截屏监听已开启
'
})
}
else
{
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
捕获截屏事件
'
})
that
.
screenImage
=
res
.
image
}
}
else
{
// 除android 之外的平台,不需要判断返回状态码
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
捕获截屏事件
'
})
}
});
if
(
uni
.
getSystemInfoSync
().
platform
!=
"
android
"
)
{
// 除android 之外的平台,直接提示监听已开启
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
截屏监听已开启
'
})
}
},
testScreenShotOff
()
{
var
that
=
this
;
uni
.
offUserCaptureScreen
(
function
(
res
)
{
console
.
log
(
res
);
});
// 提示已经开始监听,注意观察
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
截屏监听已关闭
'
})
},
testGetBatteryInfo
()
{
var
that
=
this
;
uni
.
getBatteryInfo
({
success
(
res
)
{
uni
.
showToast
({
title
:
"
当前电量:
"
+
res
.
level
+
'
%
'
,
icon
:
'
none
'
});
}
})
},
}
}
</
script
>
...
...
uni_modules/uni-screen/changelog.md
0 → 100644
浏览文件 @
270fe69f
uni_modules/uni-screen/package.json
0 → 100644
浏览文件 @
270fe69f
{
"id"
:
"uni-screen"
,
"displayName"
:
"uni-screen"
,
"version"
:
"1.0.0"
,
"description"
:
"uni-screen"
,
"keywords"
:
[
"屏幕"
],
"repository"
:
""
,
"engines"
:
{
"HBuilderX"
:
"^3.6.0"
},
"dcloudext"
:
{
"type"
:
"uts"
,
"sale"
:
{
"regular"
:
{
"price"
:
"0.00"
},
"sourcecode"
:
{
"price"
:
"0.00"
}
},
"contact"
:
{
"qq"
:
""
},
"declaration"
:
{
"ads"
:
""
,
"data"
:
""
,
"permissions"
:
""
},
"npmurl"
:
""
},
"uni_modules"
:
{
"uni-ext-api"
:{
"uni"
:
{
"onUserCaptureScreen"
:
"onUserCaptureScreen"
,
"offUserCaptureScreen"
:
"offUserCaptureScreen"
}
},
"dependencies"
:
[],
"encrypt"
:
[],
"platforms"
:
{
"cloud"
:
{
"tcb"
:
"u"
,
"aliyun"
:
"u"
},
"client"
:
{
"Vue"
:
{
"vue2"
:
"n"
,
"vue3"
:
"y"
},
"App"
:
{
"app-android"
:
"u"
,
"app-ios"
:
"u"
},
"H5-mobile"
:
{
"Safari"
:
"u"
,
"Android Browser"
:
"u"
,
"微信浏览器(Android)"
:
"u"
,
"QQ浏览器(Android)"
:
"u"
},
"H5-pc"
:
{
"Chrome"
:
"u"
,
"IE"
:
"u"
,
"Edge"
:
"u"
,
"Firefox"
:
"u"
,
"Safari"
:
"u"
},
"小程序"
:
{
"微信"
:
"u"
,
"阿里"
:
"u"
,
"百度"
:
"u"
,
"字节跳动"
:
"u"
,
"QQ"
:
"u"
,
"钉钉"
:
"u"
,
"快手"
:
"u"
,
"飞书"
:
"u"
,
"京东"
:
"u"
},
"快应用"
:
{
"华为"
:
"u"
,
"联盟"
:
"u"
}
}
}
}
}
uni_modules/uni-screen/readme.md
0 → 100644
浏览文件 @
270fe69f
# uts-screenshot-listener
### 开发文档
[
UTS 语法
](
https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html
)
[
UTS 原生插件
](
https://uniapp.dcloud.net.cn/plugin/uts-plugin.html
)
[
Hello UTS
](
https://gitcode.net/dcloud/hello-uts/-/tree/dev
)
\ No newline at end of file
uni_modules/uni-screen/utssdk/app-android/index.uts
0 → 100644
浏览文件 @
270fe69f
import {
getUniActivity
} from "io.dcloud.uts.android";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
import Build from "android.os.Build";
import FileObserver from "android.os.FileObserver";
import File from "java.io.File";
import Environment from "android.os.Environment";
import System from 'java.lang.System';
/**
* 文件监听器
*/
let screenOB: ScreenFileObserver | null = null;
/**
* 记录文件监听器上次监听的时间戳,避免重复监听
*/
let lastFileObserverTime: number = 0;
/**
* 图片被捕获的实现
*/
let imageChange: UTSCallback | null = null;
/**
* android 文件监听实现
*/
class ScreenFileObserver extends FileObserver {
/**
* 所有截屏文件的存放目录
*/
allScreen: File;
constructor(screenFile: string) {
super(screenFile)
this.allScreen = new File(screenFile);
}
override onEvent(event: Int, path?: string): void {
// 只监听文件新增事件
if (event == FileObserver.CREATE) {
let newPath: string = new File(this.allScreen, path!).getPath();
let currentTime = System.currentTimeMillis();
if ((currentTime - lastFileObserverTime) < 1000) {
// 本地截屏行为比上一次超过1000ms,才认为是一个有效的时间
return;
}
lastFileObserverTime = System.currentTimeMillis()
let ret = {
code:1,
image:newPath
}
imageChange!(ret);
}
}
}
/**
* 开启截图监听
*/
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;
let directory_screenshot: File;
// 找到设备存放截屏文件的目录
let directory_pictures = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);
let directory_dcim = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
if (Build.MANUFACTURER.lowercase() === "xiaomi") {
directory_screenshot = new File(directory_dcim, "Screenshots");
} else {
directory_screenshot = new File(directory_pictures, "Screenshots");
}
if (screenOB != null) {
screenOB!.stopWatching()
}
//开始监听
screenOB = new ScreenFileObserver(directory_screenshot.path)
screenOB!.startWatching()
// 监听成功
let ret = {
code:0
}
callback(ret);
}
/**
* 关闭截屏监听
*/
export function offUserCaptureScreen(success: (res: any) => void) {
// android 10以上,关闭监听通过移除文件监听器实现
if (screenOB != null) {
screenOB!.stopWatching()
screenOB = null
}
lastFileObserverTime = 0;
let ret = {
}
success(ret);
}
uni_modules/uni-screen/utssdk/app-ios/index.uts
0 → 100644
浏览文件 @
270fe69f
import { NotificationCenter} from 'Foundation';
import { UIApplication } from "UIKit"
/**
* 定义监听截屏事件工具类
*/
class CaptureScreenTool {
static listener?: UTSCallback;
// 监听截屏
static listenCaptureScreen(callback?: UTSCallback) {
this.listener = callback
// 注册监听截屏事件及回调方法
// target-action 回调方法需要通过 Selector("方法名") 构建
const method = Selector("userDidTakeScreenshot")
NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.userDidTakeScreenshotNotification, object = null)
}
// 捕获截屏回调的方法
// target-action 的方法前需要添加 @objc 前缀
@objc static userDidTakeScreenshot() {
const obj = new UTSJSONObject()
// 回调
this.listener?.(obj)
}
// 移除监听事件
static removeListen(callback?: UTSCallback) {
this.listener = null
NotificationCenter.default.removeObserver(this)
const obj = new UTSJSONObject()
callback?.(obj)
}
}
/**
* 开启截图监听
*/
export function onUserCaptureScreen(callback?: UTSCallback) {
CaptureScreenTool.listenCaptureScreen(callback)
}
/**
* 关闭截屏监听
*/
export function offUserCaptureScreen(callback?: UTSCallback) {
CaptureScreenTool.removeListen(callback)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录