提交 6bb96332 编写于 作者: 杜庆泉's avatar 杜庆泉

新增生命周期监听 增加示例

上级 5147afd5
......@@ -2,6 +2,8 @@
<template>
<view>
<page-head :title="title"></page-head>
<button @tap="testGoOtherActivity">跳转选择界面</button>
<image :src="selectImage" v-if="selectImage"></image>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text">
1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
......@@ -18,12 +20,13 @@
</view>
</template>
<script>
import { initAppLifecycle } from '../../uni_modules/uts-advance';
import { initAppLifecycle,goOtherActivity } from '../../uni_modules/uts-advance';
export default {
data() {
return {
title: '生命周期监听',
text: '',
selectImage:""
}
},
onLoad:function(){
......@@ -33,6 +36,22 @@
that.text = that.text += eventLog;
that.text = that.text += '\n';
});
},
methods:{
testGoOtherActivity(){
var that = this;
let ret = goOtherActivity(function(file){
// 展示捕捉到的声明周期日志
console.log(file);
that.selectImage = "file://" + file;
});
if(!ret){
uni.showToast({
title:'请授予权限后重试'
})
}
}
}
}
</script>
......
<template>
<view>
<page-head :title="title"></page-head>
<image :src="logo" mode="aspectFit" style="width: 100%;"></image>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="testLogoLoad">展示插件内置图片</button>
</view>
<image :src="logo" mode="aspectFit" style="width: 100%;"></image>
</view>
</template>
<script>
import { getLogoPath,playAssetAudio } from '../../uni_modules/uts-toast'
import { getLogoPath } from '../../uni_modules/uts-advance'
export default {
......@@ -21,13 +19,8 @@
},
onLoad:function(){
this.logo = getLogoPath()
console.log("getLogoPath");
},
methods: {
testLogoLoad(){
this.logo = getLogoPath()
}
}
}
</script>
......
......@@ -5,15 +5,28 @@ import ViewGroup from "android.view.ViewGroup";
import Gravity from "android.view.Gravity";
import Runnable from 'java.lang.Runnable';
import MediaPlayer from 'android.media.MediaPlayer';
import Log from 'android.util.Log';
import Intent from 'android.content.Intent';
import logo from "../../static/logo.png";
import PackageManager from "android.content.pm.PackageManager";
import MediaStore from "android.provider.MediaStore";
import Bundle from "android.os.Bundle";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import Activity from "android.app.Activity";
import Bitmap from "android.graphics.Bitmap";
import FileOutputStream from "java.io.FileOutputStream";
import {
onAppActivityDestroy,
onAppActivityPause,
onAppActivityResume,
onAppActivityBack,
onAppActivityStop,
onAppActivityResult,
onAppTrimMemory,
onAppConfigChange,
getUniActivity,
getAppContext
} from "io.dcloud.uts.android";
......@@ -179,7 +192,44 @@ export function playAssetAudio() {
}
export function goOtherActivity(imageDone: (event:string) => void):boolean {
// 检查相关权限是否已经具备
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 不具备权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.CAMERA), 1002)
return false;
}
onAppActivityResult((requestCode: Int, resultCode: Int, data?: Intent) => {
let eventName = "onAppActivityResult - requestCode:" + requestCode + " -resultCode:"+resultCode + " -data:"+JSON.stringify(data);
console.log(eventName);
if ((requestCode == 1001) && (resultCode == Activity.RESULT_OK)) {
if (data != null) {
let bundle = data.getExtras();
let mImageBitmap = bundle!.get("data") as Bitmap;
let bitmapPath = getUniActivity()!.getExternalCacheDir()!.getPath() + "/photo.png"
console.log(bitmapPath);
try{
mImageBitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(bitmapPath))
}catch(e){
}
imageDone(bitmapPath);
}
}
});
let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//resolveActivity 返回可处理 Intent 的第一个 Activity 组件
if (takePictureIntent.resolveActivity(getUniActivity()!.getPackageManager()) != null) {
getUniActivity()!.startActivityForResult(takePictureIntent, 1001);
}
return true;
}
/**
......@@ -188,21 +238,40 @@ export function playAssetAudio() {
*/
export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
onAppTrimMemory((level:Number) => {
let eventName = "onAppTrimMemory - " + level;
onLifecycleChange(eventName);
console.log(eventName);
});
onAppConfigChange((ret:UTSJSONObject) => {
let eventName = "onAppConfigChange - " + JSON.stringify(ret);
onLifecycleChange(eventName);
console.log(eventName);
});
/**
* activity 销毁生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
*/
onAppActivityDestroy(() => {
let eventName = "onAppActivityDestroy - " + Date.now();
let eventName = "onAppActivityDestroy";
onLifecycleChange(eventName);
console.log(eventName);
});
/**
* activity 失去焦点生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
*/
onAppActivityPause(() => {
let eventName = "onAppActivityPause - " + Date.now();
let eventName = "onAppActivityPause" ;
onLifecycleChange(eventName);
console.log(eventName);
});
......@@ -211,7 +280,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
*/
onAppActivityResume(() => {
let eventName = "onAppActivityResume - " + Date.now();
let eventName = "onAppActivityResume";
onLifecycleChange(eventName);
console.log(eventName);
});
......@@ -220,7 +289,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback
*/
onAppActivityBack(() => {
let eventName = "onAppActivityBack - " + Date.now();
let eventName = "onAppActivityBack";
onLifecycleChange(eventName);
console.log(eventName);
});
......
......@@ -40,7 +40,7 @@ class ForeService extends Service {
}
override onBind(intent?: Intent): IBinder|null{
override onBind(_intent?: Intent): IBinder|null{
return null;
}
......@@ -98,7 +98,7 @@ class ScreenReceiver extends BroadcastReceiver{
super();
}
override onReceive(context: Context, intent: Intent):void {
override onReceive(_context: Context, intent: Intent):void {
if (Intent.ACTION_SCREEN_OFF == intent.action) {
console.log("==屏幕休眠了")
}
......
{
"abis": [
"arm64-v8a"
],
"minSdkVersion": 21
}
......@@ -5,21 +5,14 @@ import {
import XToast from "com.hjq.xtoast.XToast";
import R from "io.dcloud.uni_modules.uts_toast.R";
export function getLogoPath():string{
export function showToast():string{
// new XToast<Any>(getUniActivity())
// .setDuration(1000)
// .setContentView(R.layout.window_hint)
// .setImageDrawable(android.R.id.icon, android.R.mipmap.sym_def_app_icon)
// .setText(android.R.id.message, "一秒后自动消失")
// .show();
XToast<XToast<Any>>(getUniActivity())
new XToast<XToast<unknown>>(getUniActivity())
.setDuration(1000)
.setContentView(R.layout.window_hint)
.setImageDrawable(android.R.id.icon, android.R.mipmap.sym_def_app_icon)
.setText(android.R.id.message, "一秒后自动消失")
.show()
return "hello toast"
return ""
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册