实验课程

THIS NAME

实验课程

当前位置: 首页 >> 实验课程 >> 正文

Harmony程序设计-访问后台服务获取电量信息

发布日期:2024-03-04    作者:陆其美     来源:     点击:

访问后台服务获取电量信息

一、 实验目的

1、建立后台service获取电量信息

2、建立后台与前台信息交互的接口类

3、前台page与后台service交互

二、实验设备与器件

PC机、DevEco Studio工具

三、实验内容

1. 建立后台service获取电量信息

在当前项目的entry\src\main\java目录中的包、com.XX.batteryjavacallpa上单击鼠标右键,新建一个Service Ability,将其命名为BatteryInfo。该servicez主要是提供电量获取功能,系统在config.json文件中会默认注册其为service,注册BatteryInfo Service的代码如下

MainAbilitySlice

package com.example.myapplication_dianchi.slice;

import com.example.myapplication_dianchi.MyRemote;
import com.example.myapplication_dianchi.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

public class MainAbilitySlice extends AbilitySlice {
   private String result;
   private static final String TAG = MainAbilitySlice.class.getSimpleName();
   //@Override
   private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);
   public void onStart(Intent intent) {
       super.onStart(intent);
       super.setUIContent(ResourceTable.Layout_ability_main);
       Button button=(Button) findComponentById(ResourceTable.Id_button_battery);
       button.setClickedListener(component ->startBatteryService());
   }
private IAbilityConnection connection=new IAbilityConnection() {
   @Override
   public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
       HiLog.info(LABEL_LOG, "%{public}s", "onAbilityConnectDone resultCode : " + resultCode);
       MyRemote clientRemote = (MyRemote) iRemoteObject;
       Text txt = (Text)findComponentById(ResourceTable.Id_text_helloworld);
       Text txt1= (Text)findComponentById(ResourceTable.Id_text_charing);
       result=clientRemote.getbattery();
       String[] str=result.split(System.lineSeparator());
       txt.setText(str[0]);
       txt1.setText(str[1]);
       Button btn = (Button)findComponentById(ResourceTable.Id_button_battery);
       Button btn1 = (Button)findComponentById(ResourceTable.Id_button_charing);
       btn.setText("电池容量");
       btn1.setText("电池状态");
   }

   @Override
   public void onAbilityDisconnectDone(ElementName elementName, int i) {

   }
};
private void startBatteryService()
{
   Operation operation = new Intent.OperationBuilder().withDeviceId("")
           .withBundleName("com.example.myapplication_dianchi")
           .withAbilityName("com.example.myapplication_dianchi.BatteryInfo")
           .build();
   Intent intent = new Intent();
   intent.setOperation(operation);
   connectAbility(intent, connection);
}

   @Override
   public void onActive() {
       super.onActive();
   }

   @Override
   public void onForeground(Intent intent) {
       super.onForeground(intent);
   }
}

484C

MainAbility2Slice

package com.example.myapplication_dianchi.slice;

import com.example.myapplication_dianchi.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

public class MainAbility2Slice extends AbilitySlice {
   @Override
   public void onStart(Intent intent) {
       super.onStart(intent);
       super.setUIContent(ResourceTable.Layout_ability_mainability2);
   }

   @Override
   public void onActive() {
       super.onActive();
   }

   @Override
   public void onForeground(Intent intent) {
       super.onForeground(intent);
   }
}

送到虚拟机后显示的结果

484C

ability_main.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
   xmlns:ohos="http://schemas.huawei.com/res/ohos"
   ohos:height="match_parent"
   ohos:width="match_parent"
   ohos:alignment="center"
   ohos:orientation="vertical">

   <DirectionalLayout
       xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:height="match_content"
       ohos:width="match_content"
       ohos:alignment="center"
       ohos:orientation="horizontal">
       <Text
           ohos:id="$+id:text_helloworld"
           ohos:height="match_content"
           ohos:width="match_content"
           ohos:background_element="$graphic:background_ability_main"
           ohos:layout_alignment="horizontal_center"
           ohos:text="$string:mainability_HelloWorld"
           ohos:text_size="25vp"
           />

       <Button
           ohos:id="$+id:button_battery"
           ohos:height="match_content"
           ohos:width="match_content"
           ohos:background_element="$graphic:background_ability_main"
           ohos:layout_alignment="horizontal_center"
           ohos:left_margin="40px"
           ohos:text="获取电量信息"
           ohos:text_size="25vp"
           />
   </DirectionalLayout>

   <DirectionalLayout
       xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:height="match_content"
       ohos:width="match_content"
       ohos:top_margin="50px"
       ohos:alignment="center"
       ohos:orientation="horizontal">
       <Text
           ohos:id="$+id:text_charing"
           ohos:height="match_content"
           ohos:width="match_content"
           ohos:background_element="$graphic:background_ability_main"
           ohos:layout_alignment="horizontal_center"
           ohos:text="$string:mainability_HelloWorld"
           ohos:text_size="25vp"
           />

       <Button
           ohos:id="$+id:button_charing"
           ohos:height="match_content"
           ohos:width="match_content"
           ohos:background_element="$graphic:background_ability_main"
           ohos:layout_alignment="horizontal_center"
           ohos:left_margin="40px"
           ohos:text="获取充电信息"
           ohos:text_size="25vp"
           />

   </DirectionalLayout>


</DirectionalLayout>

送到用户虚拟机后显示的结果


ability_mainability2.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
   xmlns:ohos="http://schemas.huawei.com/res/ohos"
   ohos:height="match_parent"
   ohos:width="match_parent"
   ohos:alignment="center"
   ohos:orientation="vertical">

   <Text
       ohos:id="$+id:text_helloworld"
       ohos:height="match_content"
       ohos:width="match_content"
       ohos:background_element="$graphic:background_ability_mainability2"
       ohos:layout_alignment="horizontal_center"
       ohos:text="$string:mainability2_HelloWorld"
       ohos:text_size="40vp"
       />

</DirectionalLayout>

送到虚拟机后显示的结果

undefined

2.返回电池电量及状态字符串

BatteryInfo

private String getBatteryInfo() {
   StringBuilder stringBuilder = new StringBuilder();
   boolean isCharging = getChargingStatus();
   double batteryValue = getBatteryLevel();
   stringBuilder.append(batteryValue)
           .append(" % Battery Left" + System.lineSeparator())
           .append("isCharging: ")
           .append(isCharging);
   return stringBuilder.toString();
}

界面设计的结果

undefined

3.建立后台与前台信息交互的接口类

MyRemote

public class MyRemote extends LocalRemoteObject {
   private String battery = "";
   public MyRemote() {
       super();
   }
   public String getbattery() {
       return battery;
   }
   public void setbattery(String newbattery) {
       battery = newbattery;
   }
}

界面设计的结果

undefined

4、前台pageservice交互代码如下

MainAbilitySlice

private void startBatteryService()
{
   Operation operation = new Intent.OperationBuilder().withDeviceId("")
           .withBundleName("com.example.myapplication_dianchi")
           .withAbilityName("com.example.myapplication_dianchi.BatteryInfo")
           .build();
   Intent intent = new Intent();
   intent.setOperation(operation);
   connectAbility(intent, connection);
}

界面设计的结果

undefined

5.客户端IAbilityConnection对象的实现

文件select.php

private IAbilityConnection connection=new IAbilityConnection() {
   @Override
   public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
       HiLog.info(LABEL_LOG, "%{public}s", "onAbilityConnectDone resultCode : " + resultCode);
       MyRemote clientRemote = (MyRemote) iRemoteObject;
       Text txt = (Text)findComponentById(ResourceTable.Id_text_helloworld);
       Text txt1= (Text)findComponentById(ResourceTable.Id_text_charing);
       result=clientRemote.getbattery();
       String[] str=result.split(System.lineSeparator());
       txt.setText(str[0]);
       txt1.setText(str[1]);
       Button btn = (Button)findComponentById(ResourceTable.Id_button_battery);
       Button btn1 = (Button)findComponentById(ResourceTable.Id_button_charing);
       btn.setText("电池容量");
       btn1.setText("电池状态");
   }

   @Override
   public void onAbilityDisconnectDone(ElementName elementName, int i) {

   }
};

界面设计的结果

undefined

上一条:计算机程序设计(Pyhton)-Hello World 下一条:计算机程序设计(C)-C语言指针实验

关闭