提交 8eea7a8b 编写于 作者: DCloud-yinjiacheng's avatar DCloud-yinjiacheng

Android uni-usercapturescreen 语法调整

上级 bbccccd5
......@@ -28,8 +28,7 @@
title: 'Hello',
memListener:null,
setUserCaptureScreenFlag: false,
setUserCaptureScreenText: '禁止截屏',
permissionGranted: false
setUserCaptureScreenText: '禁止截屏'
}
},
onLoad() {
......@@ -139,30 +138,14 @@
var that = this;
uni.onUserCaptureScreen(function(res) {
console.log(res);
if (uni.getSystemInfoSync().platform == "android") {
// 除android 之外的平台,不需要判断返回状态码
if(res.errCode == -1){
// 启动失败
that.permissionGranted = true;
return ;
}else if(res.errCode == 0){
uni.showToast({
icon:"none",
title:'捕获截屏事件'
})
that.screenImage = res.image
}
}else{
// 除android 之外的平台,不需要判断返回状态码
uni.showToast({
icon:"none",
title:'捕获截屏事件'
})
}
that.screenImage = res.path
});
if (uni.getSystemInfoSync().platform != "android" || that.permissionGranted) {
if (uni.getSystemInfoSync().platform != "android") {
// 除android 之外的平台,直接提示监听已开启
uni.showToast({
icon:"none",
......
......@@ -8,7 +8,7 @@ import File from "java.io.File";
import Environment from "android.os.Environment";
import System from 'java.lang.System';
import WindowManager from 'android.view.WindowManager';
import { UserCaptureScreenResult, OnUserCaptureScreenResult, SetUserCaptureScreenOptions, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen } from "../interface.uts";
import { OnUserCaptureScreenCallbackResult, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreenSuccess, SetUserCaptureScreenOptions, SetUserCaptureScreen } from "../interface.uts";
/**
* 文件监听器
......@@ -50,10 +50,7 @@ class ScreenFileObserver extends FileObserver {
lastObserverTime = currentTime;
const screenShotPath = new File(this.screenFile, path).getPath();
const res : OnUserCaptureScreenResult = {
errCode: 0,
errMsg: "onUserCaptureScreen:ok",
errSubject: "uni-onUserCaptureScreen",
const res : OnUserCaptureScreenCallbackResult = {
path: screenShotPath
}
listener?.(res);
......@@ -68,23 +65,15 @@ class ScreenFileObserver extends FileObserver {
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
// 检查相关权限是否已授予
if (ActivityCompat.checkSelfPermission(UTSAndroid.getAppContext()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 无权限,申请权限,并且告知用户监听失败
// 无权限,申请权限
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001);
// 因权限缺失导致监听失败
const res : OnUserCaptureScreenResult = {
errCode: -1,
errMsg: "onUserCaptureScreen:permission denied",
errSubject: "uni-onUserCaptureScreen"
}
callback?.(res);
return;
}
// 更新监听
listener = callback;
let directory_screenshot : File;
if (Build.MANUFACTURER.toLowerCase() === "xiaomi") {
if (Build.MANUFACTURER.toLowerCase() == "xiaomi") {
// @Suppress("DEPRECATION")
directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM), "Screenshots");
} else {
......@@ -113,11 +102,7 @@ export const offUserCaptureScreen : OffUserCaptureScreen = function (_ : UserCap
export const setUserCaptureScreen : SetUserCaptureScreen = function (option : SetUserCaptureScreenOptions) {
// 切换到UI线程
UTSAndroid.getUniActivity()?.runOnUiThread(new SetUserCaptureScreenRunnable(option.enable));
const res : UserCaptureScreenResult = {
errCode: 0,
errMsg: "setUserCaptureScreen:ok",
errSubject: "uni-setUserCaptureScreen"
}
const res : SetUserCaptureScreenSuccess = {}
option.success?.(res);
option.complete?.(res);
}
......
/**
* uni.onUserCaptureScreen/uni.offUserCaptureScreen回调参数
*/
export type OnUserCaptureScreenCallbackResult = {
/**
* 截屏文件路径(仅Android返回)
......@@ -11,30 +13,28 @@ export type OnUserCaptureScreenCallbackResult = {
*/
export type UserCaptureScreenCallback = (res : OnUserCaptureScreenCallbackResult) => void
/**
* uni.onUserCaptureScreen函数定义
* 开启截屏监听
*
* @param {UserCaptureScreenCallback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.6.8
*/
export type OnUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void
/**
* uni.offUserCaptureScreen函数定义
* 关闭截屏监听
*
* @param {UserCaptureScreenCallback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.6.8
*/
export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void
/**
* uni.setUserCaptureScreen成功回调参数
*/
......@@ -48,7 +48,6 @@ export type SetUserCaptureScreenFail = {
/**
* 错误码
* 0:成功
* -1:permission denied
* 12001:system not support
* 12010:system internal error
*/
......@@ -89,31 +88,31 @@ export type SetUserCaptureScreenOptions = {
/**
* 接口调用成功的回调函数
*/
//success : SetUserCaptureScreenSuccessCallback | null,
// success : SetUserCaptureScreenSuccessCallback | null,
success ?: SetUserCaptureScreenSuccessCallback,
/**
* 接口调用失败的回调函数
*/
//fail : SetUserCaptureScreenFailCallback | null,
// fail : SetUserCaptureScreenFailCallback | null,
fail ?: SetUserCaptureScreenFailCallback,
/**
* 接口调用结束的回调函数(调用成功、失败都会执行)
*/
//complete : SetUserCaptureScreenSuccessCallback |SetUserCaptureScreenFailCallback | null
// complete : SetUserCaptureScreenSuccessCallback | SetUserCaptureScreenFailCallback | null
complete ?: SetUserCaptureScreenCompleteCallback
}
/**
* uni.setUserCaptureScreen函数定义
* 设置防截屏
*
* @param {SetUserCaptureScreenOptions} options
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#setusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.7.3
*/
export type SetUserCaptureScreen = (options : SetUserCaptureScreenOptions) => void
interface uni {
onUserCaptureScreen : OnUserCaptureScreen,
offUserCaptureScreen : OffUserCaptureScreen,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册