APP的下载和安装

url="https://app.mi.com/download/459391?id=com.duokan.freereader&ref=appstore.mobile_download&nonce=2405532095291614074%3A26971612&appClientId=2882303761517485445&appSignature=8Qsd3QMyKvFxIWtEbwVoC8C2tMwpP0KzQA1WL6k8X2Q"
http.get(url,{},function(res,err){
    if(err){
        console.error("download_apk",err);
        toast("下载出错")
        return;
    }
    if(res.statusCode==200){
        files.writeBytes("/sdcard/1.apk", res.body.bytes())
        toast("下载完成,正在安装")
        //打开apk
        let apk = new java.io.File("/sdcard/1.apk");
        let uri = android.net.Uri.fromFile(apk);
        let intent = new Intent();
        intent.setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
        intent.setData(uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        app.startActivity(intent);
        //执行安装脚本
        _wait(()=>{
            let node=selector().text("安装").findOnce();
            if(node){
                node.click()
            }
            node=selector().textContains("完成").findOnce();
            if(node){
                home();
                return true;
            }
        },1000*60)
    }
});

App的卸载

function uninstallApk(appName){
    let pkg=app.getPackageName(appName);
    if(!pkg){
        toast("不存在需要卸载的APP");
        return;
    }
    app.uninstall(pkg);
    _wait(()=>{
        let node=selector().text("确定").findOnce();
        if(node){
            node.click()
        }
        node=selector().textContains("完成").findOnce();
        if(node){
            home();
            return true;
        }
    },1000*60)
}

关闭APP

function closeApp(packageName) {
    function closeAppClick(node){
        let point=node.bounds();
        let x=point.left+(point.right-point.left)/2
        let y=point.top+(point.bottom-point.top)/2
        click(x,y)
    }
    var name = getPackageName(packageName);
    if (!name) {
        if (getAppName(packageName)) {
            name = packageName;
        } else {
            return false;
        }
    }
    app.openAppSetting(name);
    text(app.getAppName(name)).waitFor();
    let reg=/(.*强[制行].*|.*停止.*|.*结束.*)/;
    let is_sure = selector().textMatches(reg).findOne();
    if (is_sure.enabled()) {
        closeAppClick(selector().textMatches(reg).findOne());
        selector().textMatches(/(.*确.*|.*定.*)/).findOne().click();
        sleep(1000);
        back();
    } else {
        back();
    }
}

启动APP

app.launchApp("米阅小说")

最后附上文档地址

补充

经过测试发现安装apk在高版本安卓无法使用,于是博主重新写了一篇文章关于autojs安装apk

标签: autojs

添加新评论