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

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

上级 5147afd5
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
<template> <template>
<view> <view>
<page-head :title="title"></page-head> <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-padding-wrap uni-common-mt">
<view class="uni-hello-text"> <view class="uni-hello-text">
1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。 1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
...@@ -18,12 +20,13 @@ ...@@ -18,12 +20,13 @@
</view> </view>
</template> </template>
<script> <script>
import { initAppLifecycle } from '../../uni_modules/uts-advance'; import { initAppLifecycle,goOtherActivity } from '../../uni_modules/uts-advance';
export default { export default {
data() { data() {
return { return {
title: '生命周期监听', title: '生命周期监听',
text: '', text: '',
selectImage:""
} }
}, },
onLoad:function(){ onLoad:function(){
...@@ -33,6 +36,22 @@ ...@@ -33,6 +36,22 @@
that.text = that.text += eventLog; that.text = that.text += eventLog;
that.text = that.text += '\n'; 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> </script>
......
...@@ -2,14 +2,12 @@ ...@@ -2,14 +2,12 @@
<view> <view>
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<image :src="logo" mode="aspectFit" style="width: 100%;"></image> <image :src="logo" mode="aspectFit" style="width: 100%;"></image>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="testLogoLoad">展示插件内置图片</button>
</view>
</view> </view>
</template> </template>
<script> <script>
import { getLogoPath,playAssetAudio } from '../../uni_modules/uts-toast' import { getLogoPath } from '../../uni_modules/uts-advance'
export default { export default {
...@@ -21,13 +19,8 @@ ...@@ -21,13 +19,8 @@
}, },
onLoad:function(){ onLoad:function(){
this.logo = getLogoPath() this.logo = getLogoPath()
console.log("getLogoPath");
},
methods: {
testLogoLoad(){
this.logo = getLogoPath()
}
} }
} }
</script> </script>
......
...@@ -5,15 +5,28 @@ import ViewGroup from "android.view.ViewGroup"; ...@@ -5,15 +5,28 @@ import ViewGroup from "android.view.ViewGroup";
import Gravity from "android.view.Gravity"; import Gravity from "android.view.Gravity";
import Runnable from 'java.lang.Runnable'; import Runnable from 'java.lang.Runnable';
import MediaPlayer from 'android.media.MediaPlayer'; 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 logo from "../../static/logo.png";
import PackageManager from "android.content.pm.PackageManager"; 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 { import {
onAppActivityDestroy, onAppActivityDestroy,
onAppActivityPause, onAppActivityPause,
onAppActivityResume, onAppActivityResume,
onAppActivityBack, onAppActivityBack,
onAppActivityStop,
onAppActivityResult,
onAppTrimMemory,
onAppConfigChange,
getUniActivity, getUniActivity,
getAppContext getAppContext
} from "io.dcloud.uts.android"; } from "io.dcloud.uts.android";
...@@ -179,7 +192,44 @@ export function playAssetAudio() { ...@@ -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() { ...@@ -188,21 +238,40 @@ export function playAssetAudio() {
*/ */
export function initAppLifecycle(onLifecycleChange: (event:string) => void) { 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 销毁生命周期回调 * activity 销毁生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy
*/ */
onAppActivityDestroy(() => { onAppActivityDestroy(() => {
let eventName = "onAppActivityDestroy - " + Date.now(); let eventName = "onAppActivityDestroy";
onLifecycleChange(eventName); onLifecycleChange(eventName);
console.log(eventName); console.log(eventName);
}); });
/** /**
* activity 失去焦点生命周期回调 * activity 失去焦点生命周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause
*/ */
onAppActivityPause(() => { onAppActivityPause(() => {
let eventName = "onAppActivityPause - " + Date.now(); let eventName = "onAppActivityPause" ;
onLifecycleChange(eventName); onLifecycleChange(eventName);
console.log(eventName); console.log(eventName);
}); });
...@@ -211,7 +280,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) { ...@@ -211,7 +280,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
*/ */
onAppActivityResume(() => { onAppActivityResume(() => {
let eventName = "onAppActivityResume - " + Date.now(); let eventName = "onAppActivityResume";
onLifecycleChange(eventName); onLifecycleChange(eventName);
console.log(eventName); console.log(eventName);
}); });
...@@ -220,7 +289,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) { ...@@ -220,7 +289,7 @@ export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback
*/ */
onAppActivityBack(() => { onAppActivityBack(() => {
let eventName = "onAppActivityBack - " + Date.now(); let eventName = "onAppActivityBack";
onLifecycleChange(eventName); onLifecycleChange(eventName);
console.log(eventName); console.log(eventName);
}); });
......
...@@ -40,7 +40,7 @@ class ForeService extends Service { ...@@ -40,7 +40,7 @@ class ForeService extends Service {
} }
override onBind(intent?: Intent): IBinder|null{ override onBind(_intent?: Intent): IBinder|null{
return null; return null;
} }
...@@ -98,7 +98,7 @@ class ScreenReceiver extends BroadcastReceiver{ ...@@ -98,7 +98,7 @@ class ScreenReceiver extends BroadcastReceiver{
super(); super();
} }
override onReceive(context: Context, intent: Intent):void { override onReceive(_context: Context, intent: Intent):void {
if (Intent.ACTION_SCREEN_OFF == intent.action) { if (Intent.ACTION_SCREEN_OFF == intent.action) {
console.log("==屏幕休眠了") console.log("==屏幕休眠了")
} }
......
{
"abis": [
"arm64-v8a"
],
"minSdkVersion": 21
}
...@@ -5,21 +5,14 @@ import { ...@@ -5,21 +5,14 @@ import {
import XToast from "com.hjq.xtoast.XToast"; import XToast from "com.hjq.xtoast.XToast";
import R from "io.dcloud.uni_modules.uts_toast.R"; import R from "io.dcloud.uni_modules.uts_toast.R";
export function getLogoPath():string{ export function showToast():string{
// new 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();
XToast<XToast<Any>>(getUniActivity())
.setDuration(1000) .setDuration(1000)
.setContentView(R.layout.window_hint) .setContentView(R.layout.window_hint)
.setImageDrawable(android.R.id.icon, android.R.mipmap.sym_def_app_icon) .setImageDrawable(android.R.id.icon, android.R.mipmap.sym_def_app_icon)
.setText(android.R.id.message, "一秒后自动消失") .setText(android.R.id.message, "一秒后自动消失")
.show() .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.
先完成此消息的编辑!
想要评论请 注册