SystemAPI.uvue 9.8 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<template>
    <view class="uni-container">
        <page-head :title="title"></page-head>

        <view class="uni-panel" v-for="(item, index) in list" :key="index">
            <view class="uni-panel-h" :class="item.open ? 'uni-panel-h-on' : ''" @click="triggerCollapse(index)">
                <text class="uni-panel-text">{{item.name}}</text>
                <image :src="item.pages.length > 0 ? item.open ? arrowUpIcon : arrowDownIcon : arrowRightIcon"
                    class="uni-icon"></image>
            </view>
            <view class="uni-panel-c" v-if="item.open">
                <view class="uni-navigate-item" v-for="(page,key) in item.pages" :key="key" @click="goDetailPage(page)" hover-class="uni-navigate-item-active">
                    <text class="uni-navigate-text">{{page.name}}</text>
                    <image :src="arrowRightIcon" class="uni-icon" v-if="page.url"></image>
                </view>
            </view>
        </view>
    </view>
</template>
<script lang="ts">
杜庆泉's avatar
杜庆泉 已提交
21
    import { gotoDemoActivity,sayHelloFromJar,sayHelloFromSO } from "@/uni_modules/uts-nativepage";
lizhongyi_'s avatar
lizhongyi_ 已提交
22
    import { getBatteryInfo, GetBatteryInfoOptions } from "@/uni_modules/uts-getbatteryinfo";
23 24 25 26 27 28 29 30 31
    // #ifdef APP-ANDROID
     import {
       UTSAcvitiyLifeCycleCallback,
       UTSAcvitiyKeyEventCallback,
       UTSActivityWindowCallback,
       UTSActivityCallback,
       UTSActivityComponentCallback,
       onCallbackChange
     } from '@/uni_modules/uts-syntaxcase'
lizhongyi_'s avatar
lizhongyi_ 已提交
32
    
33 34 35 36
    
     import File from 'java.io.File';
     import Intent from 'android.content.Intent';
    // #endif
lizhongyi_'s avatar
lizhongyi_ 已提交
37 38 39 40 41 42 43 44 45 46
    type Page = {
        name : string,
        url ?: string
        function ?: string
    }
    type ListItem = {
        name : string,
        open : boolean,
        pages : Page[],
    }
47 48 49 50 51 52
    export default {
        unmounted() {
          // #ifdef APP-ANDROID
          this.unRegActivityCallback()
          // #endif
        },
lizhongyi_'s avatar
lizhongyi_ 已提交
53 54 55 56 57 58 59 60 61 62
        data() {
            return {
                title: '系统API示例',

                list: [{
                        name: "设备相关",
                        open: false,
                        pages: [{
                            name: "获取电池电量",
                            function: "testGetBatteryCapacity"
63 64 65 66 67 68 69 70 71
                        }
                        // #ifdef APP-ANDROID
                        ,
                        {
                            name: "注册activity 回调方法",
                            function: "testActivityCallback"
                        },
                         // #endif
                        ] as Page[]
lizhongyi_'s avatar
lizhongyi_ 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                    },
                    {
                        name: "系统事件",
                        open: false,
                        pages: [{
                            name: "监听系统截屏",
                            url: "SystemAPI/ScreenListen/screenlisten"
                        }] as Page[]
                    },
                    {
                        name: "Alert系统弹窗",
                        open: false,
                        pages: [{
                            name: "Alert弹窗",
                            url: "SystemAPI/Alert/alert"
                        }] as Page[]
                    },
                    {
                        name: "android平台",
                        open: false,
                        pages: [{
                            name: "自定义activity(需自定义基座)",
                            function: "testGotoDemoActivity"
杜庆泉's avatar
杜庆泉 已提交
95 96 97
                        },{
                            name: "调用jar中的方法",
                            function: "testNativeJar"
杜庆泉's avatar
杜庆泉 已提交
98 99 100
                        },{
                            name: "调用so中的方法",
                            function: "testNativeSO"
lizhongyi_'s avatar
lizhongyi_ 已提交
101 102 103 104 105
                        }] as Page[]
                    }
                ] as ListItem[],
                arrowUpIcon: '/static/icons/arrow-up.png',
                arrowDownIcon: '/static/icons/arrow-down.png',
106 107 108
                arrowRightIcon: '/static/icons/arrow-right.png',
                callback: [] as Any[],
                cbText: '',
lizhongyi_'s avatar
lizhongyi_ 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
            }
        },
        methods: {
            triggerCollapse(index: number) {
                for (var i = 0; i < this.list.length; ++i) {
                    if (index == i) {
                        this.list[i].open = !this.list[i].open;
                    } else {
                        this.list[i].open = false;
                    }
                }
            },
            goDetailPage(e: Page) {
                if (e.function != null) {
                    const funName = e.function
                    switch (funName) {
                        case 'testGetBatteryCapacity':
                            this.testGetBatteryCapacity()
                            break
128 129 130
                        case 'testActivityCallback':
                            this.testActivityCallback()
                            break
lizhongyi_'s avatar
lizhongyi_ 已提交
131 132
                        case 'testGotoDemoActivity':
                            this.testGotoDemoActivity()
杜庆泉's avatar
杜庆泉 已提交
133
                            break
杜庆泉's avatar
杜庆泉 已提交
134 135
                        case 'testNativeJar':
                            this.testNativeJar()
lizhongyi_'s avatar
lizhongyi_ 已提交
136
                            break
杜庆泉's avatar
杜庆泉 已提交
137 138 139
                        case 'testNativeSO':
                            this.testNativeSO()
                            break
lizhongyi_'s avatar
lizhongyi_ 已提交
140 141 142 143 144 145 146 147
                    }
                    return
                }
                uni.navigateTo({
                    url: `/pages/${e.url}`
                })
            },

148
            testGetBatteryCapacity() {
lizhongyi_'s avatar
lizhongyi_ 已提交
149 150 151 152 153 154 155 156
                getBatteryInfo({
                    success(res) {
                        uni.showToast({
                            title: "当前电量:" + (res as UTSJSONObject)['level'] + '%',
                            icon: 'none'
                        });
                    }
                } as GetBatteryInfoOptions)
杜庆泉's avatar
杜庆泉 已提交
157
            },
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
            
            
            // #ifdef APP-ANDROID
            testActivityCallback(){
              this.activityCallback()
            },
            activityCallback() {
              uni.showToast({
                  title: "注册成功,请尝试切换到其他app,在切换回来",
                  icon: 'none'
              });
              var that = this
              onCallbackChange(function (eventLog : string) {
                // 展示捕捉到的声明周期日志
                let nextLine = that.cbText + eventLog
                that.cbText = nextLine
                let nextLineFlag = that.cbText + '\n'
                that.cbText = nextLineFlag
                console.log(that.cbText)
                uni.showToast({
                    title: "activity 回调方法:" +eventLog,
                    icon: 'none'
                });
              })
              let index = getCurrentPages().length - 1
              let page = getCurrentPages()[index]
              this.callback.push(new UTSAcvitiyLifeCycleCallback())
              this.callback.push(new UTSActivityWindowCallback())
              this.callback.push(new UTSAcvitiyKeyEventCallback())
              this.callback.push(new UTSActivityCallback(), page.route)
              this.callback.push(new UTSActivityComponentCallback())
              this.callback.forEach((value) => {
                if (value instanceof UTSAcvitiyLifeCycleCallback) {
                  UTSAndroid.onActivityCallback(value,'tabBar')
                }
                if (value instanceof UTSActivityWindowCallback) {
                  UTSAndroid.onActivityCallback(value)
                }
                if (value instanceof UTSAcvitiyKeyEventCallback) {
                  UTSAndroid.onActivityCallback(value)
                }
                if (value instanceof UTSActivityCallback) {
                  UTSAndroid.onActivityCallback(value)
                }
                if (value instanceof UTSActivityComponentCallback) {
                  UTSAndroid.onActivityCallback(value)
                }
            
              })
            },
            unRegActivityCallback() {
              this.callback.forEach((value) => {
            
                if (value instanceof UTSAcvitiyLifeCycleCallback) {
                  UTSAndroid.offActivityCallback(value)
                }
                if (value instanceof UTSActivityWindowCallback) {
                  UTSAndroid.offActivityCallback(value)
                }
                if (value instanceof UTSAcvitiyKeyEventCallback) {
                  UTSAndroid.offActivityCallback(value)
                }
                if (value instanceof UTSActivityCallback) {
                  UTSAndroid.offActivityCallback(value)
                }
                if (value instanceof UTSActivityComponentCallback) {
                  UTSAndroid.offActivityCallback(value)
                }
              })
            },
            // #endif
            
杜庆泉's avatar
杜庆泉 已提交
230 231 232 233 234 235
            testNativeJar() {
                let ret = sayHelloFromJar();
                uni.showToast({
                    icon: 'none',
                    title: '来自jar中的返回值:' + ret
                })
杜庆泉's avatar
杜庆泉 已提交
236 237 238 239 240 241 242 243 244 245
            },
            
            testNativeSO() {
                let ret = sayHelloFromSO();
                uni.showToast({
                    icon: 'none',
                    title: '来自so中的返回值:' + ret
                })
            },
            
lizhongyi_'s avatar
lizhongyi_ 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            testGotoDemoActivity() {
                let ret = gotoDemoActivity();
                if (!ret) {
                    uni.showToast({
                        icon: 'none',
                        title: '需要在自定义基座中运行'
                    })
                }
            }
        }
    }
</script>

<style>
    @import '@/common/uni-uvue.css';

</style>