• 【JAVA UI】HarmonyOS怎么判断Service怎么在后台运行


     参考资料

    参考AbilityisTerminating()的方法

    api讲解

    isTerminating()

    public boolean isTerminating()

    检查当前能力是否正在被销毁。

    如果您在该能力上调用了 terminateAbility() 或其他人请求销毁该能力,则该能力将被销毁。

    Returns:

    如果当前能力正在被销毁,则返回true;否则返回false。

    代码运行

    MyApplication代码实现

    1. package com.newdemo.myapplication;
    2. import ohos.aafwk.ability.Ability;
    3. import ohos.aafwk.ability.AbilityPackage;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. public class MyApplication extends AbilityPackage {
    7. private static MyApplication myApplication;
    8. private List abilities=new ArrayList<>();
    9. @Override
    10. public void onInitialize() {
    11. super.onInitialize();
    12. myApplication=this;
    13. }
    14. public static MyApplication getsingleton(){
    15. return myApplication;
    16. }
    17. public List getAbilities() {
    18. return abilities;
    19. }
    20. }

    新建一个ServiceAbility,然后在onStart的方法中把ServiceAbility添加进去

    MyApplication.getsingleton().getAbilities().add(this);

    1. package com.newdemo.myapplication.slice;
    2. import com.newdemo.myapplication.MyApplication;
    3. import ohos.aafwk.ability.Ability;
    4. import ohos.aafwk.content.Intent;
    5. import ohos.app.Context;
    6. import ohos.rpc.IRemoteObject;
    7. import ohos.hiviewdfx.HiLog;
    8. import ohos.hiviewdfx.HiLogLabel;
    9. public class ServiceAbility extends Ability {
    10. private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    11. @Override
    12. public void onStart(Intent intent) {
    13. HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
    14. super.onStart(intent);
    15. MyApplication.getsingleton().getAbilities().add(this);
    16. }
    17. @Override
    18. public void onBackground() {
    19. super.onBackground();
    20. HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
    21. }
    22. @Override
    23. public void onStop() {
    24. super.onStop();
    25. HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
    26. }
    27. @Override
    28. public void onCommand(Intent intent, boolean restart, int startId) {
    29. }
    30. @Override
    31. public IRemoteObject onConnect(Intent intent) {
    32. return null;
    33. }
    34. @Override
    35. public void onDisconnect(Intent intent) {
    36. }
    37. }

    绘画xml布局

    在xml布局中绘画两个text组件,一个组件用于启动ServiceAbility,另外一个组件用于判断ServiceAbility是否在后台运行,代码如下

    1. <DirectionalLayout
    2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    3. ohos:height="match_parent"
    4. ohos:width="match_parent"
    5. ohos:alignment="top"
    6. ohos:orientation="vertical">
    7. <Text
    8. ohos:id="$+id:text_helloworld"
    9. ohos:height="100vp"
    10. ohos:text_alignment="center"
    11. ohos:width="match_parent"
    12. ohos:background_element="$graphic:background_ability_main"
    13. ohos:layout_alignment="horizontal_center"
    14. ohos:text="启动服务"
    15. ohos:text_size="40vp"
    16. />
    17. <Text
    18. ohos:id="$+id:isstartservice"
    19. ohos:height="100vp"
    20. ohos:width="match_parent"
    21. ohos:text_alignment="center"
    22. ohos:background_element="#ed6262"
    23. ohos:layout_alignment="horizontal_center"
    24. ohos:text="是否存在service"
    25. ohos:text_size="40vp"
    26. />
    27. DirectionalLayout>

    mainAbilitySlice代码实现

    分别实现启动ServiceAbility和判断当前ServiceAbility是否在后台运行,代码如下

    1. package com.newdemo.myapplication.slice;
    2. import com.newdemo.myapplication.MyApplication;
    3. import com.newdemo.myapplication.ResourceTable;
    4. import ohos.aafwk.ability.AbilitySlice;
    5. import ohos.aafwk.ability.RunningProcessInfo;
    6. import ohos.aafwk.content.Intent;
    7. import ohos.aafwk.content.Operation;
    8. import ohos.agp.components.Component;
    9. import ohos.hiviewdfx.HiLog;
    10. import ohos.hiviewdfx.HiLogLabel;
    11. import java.util.List;
    12. public class MainAbilitySlice extends AbilitySlice {
    13. private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    14. @Override
    15. public void onStart(Intent intent) {
    16. super.onStart(intent);
    17. super.setUIContent(ResourceTable.Layout_ability_main);
    18. //todo 启动服务
    19. findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
    20. @Override
    21. public void onClick(Component component) {
    22. Intent intent=new Intent();
    23. Operation operation=new Intent.OperationBuilder()
    24. .withBundleName(getBundleName())
    25. .withAbilityName(ServiceAbility.class.getName())
    26. .build();
    27. intent.setOperation(operation);
    28. startAbility(intent);
    29. }
    30. });
    31. //todo 判断serviceAbility是否在后台运行
    32. findComponentById(ResourceTable.Id_isstartservice).setClickedListener(new Component.ClickedListener() {
    33. @Override
    34. public void onClick(Component component) {
    35. if( MyApplication.getsingleton().getAbilities().size()>0){
    36. if( MyApplication.getsingleton().getAbilities().get(0).isTerminating()){
    37. HiLog.info(LABEL_LOG, "该服务不在后台运行");
    38. }else {
    39. HiLog.info(LABEL_LOG, "该服务在后台运行");
    40. }
    41. }else {
    42. HiLog.info(LABEL_LOG, "该服务不在后台运行");
    43. }
    44. }
    45. });
    46. }
    47. @Override
    48. public void onActive() {
    49. super.onActive();
    50. }
    51. @Override
    52. public void onForeground(Intent intent) {
    53. super.onForeground(intent);
    54. }
    55. }

    运行效果

    cke_20047.png

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    asp毕业设计——基于C#+asp.net+sqlserver作业审阅系统设计与实现(毕业论文+程序源码)——作业审阅系统
    数据表插入数据insert into
    c++编程实例
    【flutter / dart 版本】Websocket获取B站直播间弹幕教程——基于B站直播开发平台
    并查集(路径压缩)
    力扣第 305 场周赛复盘
    国内首款研发领域 AI 项目管理工具发布:PingCode AI
    HCIP认证笔记(填空)
    客户收到报价后突然安静了,怎么办?
    一文带你了解webrtc基本原理(动手实现1v1视频通话)
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126240107