isTerminating()
公共布尔 isTerminating()
检查当前产能是否正在被包装。
如果你在该能力上调用了Ability()或其他人的能力,则该能力将被不同。
回报:
如果当前可能是正确的,则返回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<>();
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");
public void onStart(Intent intent) {
HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
super.onStart(intent);
MyApplication.getsingleton().getAbilities().add(this);
}
public void onBackground() {
super.onBackground();
HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
}
public void onStop() {
super.onStop();
HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
}
public void onCommand(Intent intent, boolean restart, int startId) {
}
public IRemoteObject onConnect(Intent intent) {
return null;
}
public void onDisconnect(Intent intent) {
}
}
绘画xml布局
在xml中显示调用两个文本组件,一个组件启动ServiceAbility,另外一个组件用于后台代码,如下所示
<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"
/>
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");
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
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);
}
});
findComponentById(ResourceTable.Id_isstartservice).setClickedListener(new Component.ClickedListener() {
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, "该服务不在后台运行");
}
}
});
}
public void onActive() {
super.onActive();
}
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
运行效果
原作者: Mayism