1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 17 18let storage = LocalStorage.getShared(); 19const TAG: string = '[testTag] ExtensionPage' 20 21@Entry(storage) 22@Component 23struct Extension { 24 @State message: string = 'UIExtension Provider'; 25 private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 26 27 onPageShow() { 28 console.info(TAG, 'show'); 29 // if u wanna use setReceiveDataCallback, you should be a system app. 30 // this.session?.setReceiveDataCallback((data) => { 31 // this.message = JSON.stringify(data); 32 // }) 33 } 34 35 build() { 36 Row() { 37 Column() { 38 Text(this.message) 39 .fontSize(20) 40 .fontWeight(FontWeight.Bold) 41 .textAlign(TextAlign.Center) 42 43 Button("send data") 44 .width('80%') 45 .type(ButtonType.Capsule) 46 .margin({ 47 top: 20 48 }) 49 .onClick(() => { 50 this.session?.sendData({ "data": 543321 }); 51 }) 52 53 Button("terminate self") 54 .width('80%') 55 .type(ButtonType.Capsule) 56 .margin({ 57 top: 20 58 }) 59 .onClick(() => { 60 this.session?.terminateSelf(); 61 storage.clear(); 62 }) 63 64 Button("terminate self with result") 65 .width('80%') 66 .type(ButtonType.Capsule) 67 .margin({ 68 top: 20 69 }) 70 .onClick(() => { 71 this.session?.terminateSelfWithResult({ 72 resultCode: 0, 73 want: { 74 bundleName: "com.ohos.uiextensionprovider", 75 parameters: { "result": 123456 } 76 } 77 }) 78 }) 79 } 80 } 81 .height('100%') 82 } 83}