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

​参考资料

参考AbilityisTerminating()的方法

 

api讲解

isTerminating()

public boolean isTerminating()

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

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

Returns:

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

 

代码运行

MyApplication代码实现
package com.newdemo.myapplication;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityPackage;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends AbilityPackage {
    private static    MyApplication  myApplication;
    private List<Ability> abilities=new ArrayList<>();
    @Override
    public void onInitialize() {
        super.onInitialize();
        myApplication=this;
    }
    public static MyApplication  getsingleton(){
        return  myApplication;
    }

    public List<Ability> getAbilities() {
        return abilities;
    }
}
新建一个ServiceAbility,然后在onStart的方法中把ServiceAbility添加进去

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

package com.newdemo.myapplication.slice;

import com.newdemo.myapplication.MyApplication;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class ServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
        super.onStart(intent);
        MyApplication.getsingleton().getAbilities().add(this);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
    }
    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
    }
    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }

}
绘画xml布局

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

<?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="top"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="100vp"
        ohos:text_alignment="center"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="启动服务"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:isstartservice"
        ohos:height="100vp"
        ohos:width="match_parent"
        ohos:text_alignment="center"
        ohos:background_element="#ed6262"
        ohos:layout_alignment="horizontal_center"
        ohos:text="是否存在service"
        ohos:text_size="40vp"
        />
</DirectionalLayout>
mainAbilitySlice代码实现

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

package com.newdemo.myapplication.slice;

import com.newdemo.myapplication.MyApplication;
import com.newdemo.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.RunningProcessInfo;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //todo 启动服务
        findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent=new Intent();
                Operation operation=new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(ServiceAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);

            }
        });
        //todo 判断serviceAbility是否在后台运行
        findComponentById(ResourceTable.Id_isstartservice).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(   MyApplication.getsingleton().getAbilities().size()>0){
                    if( MyApplication.getsingleton().getAbilities().get(0).isTerminating()){
                        HiLog.info(LABEL_LOG, "该服务不在后台运行");
                    }else {
                        HiLog.info(LABEL_LOG, "该服务在后台运行");
                    }
                }else {
                    HiLog.info(LABEL_LOG, "该服务不在后台运行");
                }
            }
        });
    }

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

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

运行效果

cke_20047.png

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

posted @ 2022-08-10 08:41  华为开发者论坛  阅读(71)  评论(0编辑  收藏  举报