utsAndroid.uvue 9.6 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1
<template>
2
  <!-- #ifdef APP-ANDROID -->
lizhongyi_'s avatar
lizhongyi_ 已提交
3
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
4 5 6 7 8 9 10 11 12
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <view>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text"> 逐一点击执行,观察测试反馈 </view>
      </view>

      <button @click="getAppContextClick">getAppContext</button>
      <button @click="getUniActivityClick">getUniActivity</button>
杜庆泉's avatar
杜庆泉 已提交
13
      <button @click="getJavaClassClick">getJavaClass</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15 16 17 18 19 20
      <button @click="getAppTempPathClick">getAppTempPath</button>
      <button @click="typeofClick">typeof</button>
      <button @click="arrayPermissionFlowClick">组权限申请流程测试</button>
      <button @click="singlePermissionFlowClick">单权限申请流程测试</button>
      <button @click="dispatchAsyncClick">任务分发测试</button>
      <button @click="pathTestClick">路径转换测试</button>
      <button @click="privacyStateClick">隐私协议状态测试</button>
M
mahaifeng 已提交
21 22
      <button @click="privacyStateCallBackClick">隐私协议回调测试</button>
      <button @click="privacyStateCallBackClick">隐私协议回调测试</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text">
          1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
        </view>
        <view class="uni-hello-text">
          2. 手动切换其他APP再返回,可在控制台和界面观察事件日志
        </view>
      </view>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="text-box" scroll-y="true">
          <text>{{ text }}</text>
        </view>
      </view>
      <button @click="gotoSystemPermissionActivityClick">
        手动申请权限测试
      </button>
      <button @tap="testGoOtherActivity">跳转拍照界面</button>
      <button @tap="testUnRegLifecycle">取消注册周期函数</button>
      <image :src="selectImage" v-if="selectImage"></image>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text"> 获取设备信息,观察是否符合预期 </view>
      </view>
      <button @tap="getDeviceInfoClick">获取设备基础信息</button>
46
      <button @tap="getFileProviderUriClick">使用外部应用访问私有文件</button>
M
mahaifeng 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      <!-- #ifdef APP-ANDROID -->
        <button @tap="activityCallback">注册activity 回调方法</button>
        <view class="uni-padding-wrap uni-common-mt">
          <view class="uni-hello-text">
            点击注册activity 回调方法后,可以手动切换其他APP再返回,可在控制台和界面观察事件日志
          </view>
        </view>
        <view class="uni-padding-wrap uni-common-mt">
          <view class="text-box" scroll-y="true">
            <text>{{ cbText }}</text>
          </view>
        </view>
        <button @tap="unRegActivityCallback">取消注册activity 回调方法</button>

      <!-- #endif -->
     
DCloud-WZF's avatar
DCloud-WZF 已提交
63 64 65 66
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
lizhongyi_'s avatar
lizhongyi_ 已提交
67
  <!-- #endif -->
lizhongyi_'s avatar
lizhongyi_ 已提交
68 69 70
</template>

<script>
71
  <!-- #ifdef APP-ANDROID -->
DCloud-WZF's avatar
DCloud-WZF 已提交
72 73 74
import {
  getAppContextTest,
  getUniActivityTest,
杜庆泉's avatar
杜庆泉 已提交
75
  getJavaClassTest,
DCloud-WZF's avatar
DCloud-WZF 已提交
76 77 78 79 80 81 82 83 84 85 86 87
  getAppTempPathTest,
  typeofClickTest,
  gotoSystemPermissionActivityTest,
  arrayPermissionFlowTest,
  singlePermissionFlowTest,
  dispatchAsyncTest,
  convert2AbsFullPathTest,
  unRegLifecycle,
  initAppLifecycle,
  gotoCameraTake,
  getDeviceInfoTest,
  privacyStateTest,
M
mahaifeng 已提交
88
  privacyStateCallBackTest
DCloud-WZF's avatar
DCloud-WZF 已提交
89
} from '@/uni_modules/uts-platform-api'
M
mahaifeng 已提交
90 91 92 93 94 95 96 97 98 99 100 101
// #ifdef APP-ANDROID
import {
    UTSAcvitiyLifeCycleCallback,
    UTSAcvitiyKeyEventCallback,
    UTSActivityWindowCallback,
    UTSActivityCallback,
    UTSActivityComponentCallback,
    onCallbackChange
} from '@/uni_modules/uts-syntaxcase/utssdk/app-android/index.uts'
// #endif


杜庆泉's avatar
杜庆泉 已提交
102
import File from 'java.io.File';
103 104
import Intent from 'android.content.Intent';

DCloud-WZF's avatar
DCloud-WZF 已提交
105 106 107 108 109 110 111
/**
 * 测试在页面生命周期之外,使用api
 */
export default {
  data() {
    return {
      text: '',
M
mahaifeng 已提交
112
      cbText:'',
DCloud-WZF's avatar
DCloud-WZF 已提交
113
      selectImage: '',
M
mahaifeng 已提交
114
      callbackIds: [] as string[]
DCloud-WZF's avatar
DCloud-WZF 已提交
115 116
    }
  },
M
mahaifeng 已提交
117 118 119 120 121 122
  unmounted() {
    // #ifdef APP-ANDROID
    this.unRegActivityCallback()
    // #endif
    
  },
DCloud-WZF's avatar
DCloud-WZF 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  onLoad: function () {
    let that = this
    initAppLifecycle(function (eventLog) {
      // 展示捕捉到的声明周期日志
      let nextLine = that.text + eventLog
      that.text = nextLine
      let nextLineFlag = that.text + '\n'
      that.text = nextLineFlag
    })
  },
  methods: {
    privacyStateClick() {
      privacyStateTest(function (ret, desc) {
        if (ret) {
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: '失败:' + desc,
          })
        }
      })
    },
M
mahaifeng 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    privacyStateCallBackClick() {
      privacyStateCallBackTest(function(ret, desc) {
        if (ret) {
          uni.showToast({
            title: '测试通过'
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: '失败:' + desc
          })
        }
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    getDeviceInfoClick() {
      this.text = getDeviceInfoTest()
    },
    testGoOtherActivity() {
      var that = this
      let ret = gotoCameraTake(function (file) {
        // 展示捕捉到的声明周期日志
        console.log(file)
        that.selectImage = 'file://' + file
      })

      if (!ret) {
        uni.showToast({
          icon: 'none',
          title: '测试失败',
        })
      }
    },
    testUnRegLifecycle() {
      // 取消注册生命周期
      unRegLifecycle()
    },
杜庆泉's avatar
杜庆泉 已提交
184 185 186 187 188 189 190 191 192 193 194 195
    getJavaClassClick() {
      if (getJavaClassTest()) {
      	uni.showToast({
      		title: '测试通过'
      	})
      } else {
      	uni.showToast({
      		icon: 'error',
      		title: '测试失败'
      	})
      }
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
196 197 198 199 200 201 202 203 204 205 206 207
    getAppContextClick() {
      if (getAppContextTest()) {
        uni.showToast({
          title: '测试通过',
        })
      } else {
        uni.showToast({
          icon: 'error',
          title: '测试失败',
        })
      }
    },
杜庆泉's avatar
杜庆泉 已提交
208

DCloud-WZF's avatar
DCloud-WZF 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    getUniActivityClick() {
      if (getUniActivityTest()) {
        uni.showToast({
          title: '测试通过',
        })
      } else {
        uni.showToast({
          icon: 'error',
          title: '测试失败',
        })
      }
    },
    pathTestClick() {
      if (convert2AbsFullPathTest()) {
        uni.showToast({
          title: '测试通过',
        })
      } else {
        uni.showToast({
          icon: 'error',
          title: '测试失败',
        })
      }
    },
233
    getFileProviderUriClick() {
杜庆泉's avatar
杜庆泉 已提交
234
      let file = new File(UTSAndroid.getResourcePath("static/logo.png"))
235 236 237 238 239 240 241
      const uri = UTSAndroid.getFileProviderUri(file)
      console.log("uri",uri.toString())
      const intent = new Intent(Intent.ACTION_VIEW, uri)
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 添加权限标志 
      const context = UTSAndroid.getUniActivity()!;
      context.startActivity(intent);
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    getAppTempPathClick() {
      if (getAppTempPathTest()) {
        uni.showToast({
          title: '测试通过',
        })
      } else {
        uni.showToast({
          icon: 'error',
          title: '测试失败',
        })
      }
    },
    dispatchAsyncClick() {
      dispatchAsyncTest(function (ret, desc) {
        if (ret) {
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: '失败:' + desc,
          })
        }
      })
    },
    typeofClick() {
      if (typeofClickTest()) {
        uni.showToast({
          title: '测试通过',
        })
      } else {
        uni.showToast({
          icon: 'error',
          title: '测试失败',
        })
      }
    },
杜庆泉's avatar
杜庆泉 已提交
280

DCloud-WZF's avatar
DCloud-WZF 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    gotoSystemPermissionActivityClick() {
      gotoSystemPermissionActivityTest()
    },
    arrayPermissionFlowClick() {
      arrayPermissionFlowTest(function (ret, desc) {
        if (ret) {
          uni.showToast({
            icon: 'none',
            title: '测试通过',
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: '失败:' + desc,
          })
        }
      })
    },
    singlePermissionFlowClick() {
      singlePermissionFlowTest(function (ret, desc) {
        if (ret) {
          uni.showToast({
            icon: 'none',
            title: '测试通过',
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: '失败:' + desc,
          })
        }
      })
    },
M
mahaifeng 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    // #ifdef APP-ANDROID
    activityCallback(){
      var that = this
      onCallbackChange(function (eventLog:string) {
        // 展示捕捉到的声明周期日志
        let nextLine = that.cbText + eventLog
        that.cbText = nextLine
        let nextLineFlag = that.cbText + '\n'
        that.cbText = nextLineFlag
      })
      let reg1= UTSAndroid.registerActivityCallback( new UTSAcvitiyLifeCycleCallback())
      let reg2=UTSAndroid.registerActivityCallback(new UTSActivityWindowCallback())
      let reg3=UTSAndroid.registerActivityCallback(new UTSAcvitiyKeyEventCallback())
      let reg4= UTSAndroid.registerActivityCallback(new UTSActivityCallback())
      let reg5= UTSAndroid.registerActivityCallback(new UTSActivityComponentCallback())
      this.callbackIds.push(reg1)
      this.callbackIds.push(reg2)
      this.callbackIds.push(reg3)
      this.callbackIds.push(reg4)
      this.callbackIds.push(reg5)
    },
    unRegActivityCallback(){
       this.callbackIds.forEach((value)=>{
         console.log(value)
         UTSAndroid.unRegisterActivityCallback(value)
       })
    }
    // #endif

DCloud-WZF's avatar
DCloud-WZF 已提交
343 344
  },
}
345
<!-- #endif -->
lizhongyi_'s avatar
lizhongyi_ 已提交
346 347 348
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
349 350 351 352
.testButton {
  width: 100%;
}
</style>