回答
可以试试这个~
这里面用了几个 hamibot-starter 中的常量,如果你没有使用hamibot-starter,那你可能需要自己手动定义这个变量,例如:
export const SHORT_WAIT_MS = 300;
export const LONG_WAIT_MS = 1000;
另外这里面用的是TS,如果你使用的是JS来编写代码,在使用的时候可能需要去掉类型注释。
import { LONG_WAIT_MS, SHORT_WAIT_MS } from "../global";
let stopStrings = ["强制", "结束", "停止"];
export function force_stop(packageName: string) {
let appName = app.getAppName(packageName);
if (appName === null) {
console.error("app not found");
return;
}
app.openAppSetting(packageName);
textContains(appName).waitFor();
console.debug("open app setting page");
let stopButton: UiObject | null = null;
for (let targetString of stopStrings) {
let searchResult = textContains(targetString).findOne(SHORT_WAIT_MS);
if (searchResult !== null) {
stopButton = searchResult;
break;
}
}
if (stopButton === null) {
console.error("can not stop app, please stop the application manually");
return;
}
let position = stopButton.bounds();
click(position.centerX(), position.centerY());
console.debug("pressed stop button");
sleep(LONG_WAIT_MS);
let confirm_button = textContains("强行停止").findOne(SHORT_WAIT_MS); // for huawei
if (confirm_button !== null) {
click(
confirm_button.bounds().centerX(),
confirm_button.bounds().centerY()
);
console.debug("force stop app");
return;
}
confirm_button = textContains("确认").findOne(SHORT_WAIT_MS); // for other devices
if (confirm_button !== null) {
click(
confirm_button.bounds().centerX(),
confirm_button.bounds().centerY()
);
console.debug("force stop app");
return;
}
console.error(
"can not find stop button, please stop the application manually"
);
}
1
closeApp(packageName){
/**
* 方案一:打开应用详情页强制停止
* 方案二:不断返回首页
* 方案三:root下shell
* shell('am force-stop ' + PACKAGE_NAME, true)
*/
log(currentActivity())
app.openAppSetting(packageName)
this.sleep()
this.click(textEndsWith('停止'))
this.sleep()
this.click(textEndsWith('停止'))
},
0