[触动精灵] 零基础小白学触动13-16

小知识:触动精灵载入其他lua文件

require 以前我一直以为require只能加载模块文件 但是其实可以加载普通的其他lua文件  用法还是不加扩展名

触动精灵加载文件 都用require就可


13 - 自动打开和关闭应用


小知识:触动下开启app和关闭app

查看对应app的包名 

image

官方的runApp函数还可能存在无法启动app的情况 所以我就给封装了一下 万一出现无法启动 包名没有安装等情况 就日志记录 并且有弹窗提

下面代码  没有traceprint try catch 等函数本体

--运行app 一旦传入的包名不符合要求或者app根本没有安装 或者启动失败 会再次尝试 如果依然不行 则会报错 有提示窗口提示
function runAppEx(pid)
    local result=-1
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            pid=pid or ""
            pid=trim(pid)
            if pid=="" then
                error("runAppEx参数包名为空字符串 请检查")
            else
                
                result = runApp(pid);    
                mSleep(2 * 1000);--官方推荐等待时间是2-3秒
                if result == 0 then
                    --closeApp("com.apple.weather");    
                elseif result == 7 then
                    error("runAppEx找不到对应的包名 " .. tostring(pid) .. " 请检查")
                else
                    --再次尝试启动
                    result=-1
                    result = runApp(pid);  
                    mSleep(2 * 1000);--官方推荐等待时间是2-3秒
                    if result == 0 then
                        --closeApp("com.apple.weather");    
                    elseif result == 7 then
                        error("手机中没有安装对应的包名 " .. tostring(pid) .. " 请检查")
                    else
                        error("无法启动对应的包名 " .. tostring(pid) .. " 请检查")
                    end
                end
            end
            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("runAppEx") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end


习惯性的开启指定app的代码 点击home键的函数 下面有提到

--点击两下Home 以防万一 主要是回到手机桌面再执行启动app操作 可以避免一些意外
pid="com.android.settings"
for i=1,2 do
    keypressHome()
    mSleep(500)
end
runAppEx(pid)

关闭指定app

closeApp(pid)  --如果为了稳妥 可以回到桌面再关闭 或者关闭之后 再判断下 聊胜于无

--关闭app 
function closeAppEx(pid)
    local result=-1
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            pid=pid or ""
            pid=trim(pid)
            if pid=="" then
                error("closeAppEx参数包名为空字符串 请检查")
            else
                --判断下当前app是否运行 是则关闭 不是则不操作
                if appIsRunning(pid)==1 then
                    closeApp(pid)
                end
            end
            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("closeAppEx") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end


小知识:关于触动下点击Home键的操作 要看当前系统和版本来进行对应的操作

http://www.touchsprite.com/helpdoc#/doc?id=71


config={}
--版本问题 触动版本和系统的版本 安卓还是IOS 
config["tsver"]=tonumber(string.sub(getTSVer(), 1, 1)..string.sub(getTSVer(), 3,3)..string.sub(getTSVer(), 5,5))
config["sysver"]=tonumber(string.sub(getOSVer(), 1, 1)..string.sub(getOSVer(), 3,3)..string.sub(getOSVer(), 5, 5))
config["systype"]=getOSType()

----traceprint try catcha不写了

--点击一次home键
--支持 需要配置表的 config["systype"] config["sysver"]配合
function keypressHome()

    return try{
        function ()            
            --下面代码随便写 有可能抛出异常即可            
            if config["systype"]=="android" then
                pressHomeKey();
            else
                if config["sysver"]==700 then
                    pressHomeKey(0);    --按下抬起 Home 键一次
                elseif     config["sysver"]==600 then
                    pressHomeKey(0);    --按下 Home 键
                    pressHomeKey(1);    --抬起 Home 键
                elseif     config["sysver"]>=710 then    
                    pressHomeKey(0);    --按下 Home 键
                    pressHomeKey(1);    --抬起 Home 键
                else
                    pressHomeKey(0);    --按下 Home 键
                    pressHomeKey(1);    --抬起 Home 键
                end
            end
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("keypressHome") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end

14 - 脚本信息提示


没什么说的

dialog 可以设置第二个参数来控制显示时间 如果为0 则一直显示

toast() 这个函数以前也说过 因为是异步传输 不是实时显示 每次使用这个函数后面必须跟2-3秒延时  保证显示



15 - 逐行读取输入文本

读写文件 TS库已经封装好了 不过我为了排错和用起来熟悉 就用熟悉的函数名封装了一下这些函数

http://www.touchsprite.com/helpdoc#/doc?id=495


function  fileExist(path)
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result1,result2            
            if path=="" or path==nil then
                error("fileExist路径为空 请检查")
            else
                result1,result2=isFileExist(path)
                return result1,result2
            end

        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("fileExist") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end
--读取指定路径文件全部内容 成功返回内容 读取失败返回false 注意f是小写 因为readFile 是TS库的读取文件内容到表内
function  readfile(path)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result
            if path=="" or path==nil then
                error("readfile路径为空 请检查")
            else
                result=readFileString(path)
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("readfile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end
--把表元素的作为文件的一行覆盖写入到文件里面
function  writetabletofile(path,temptable)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result
            --temptable=temptable or {}
            if type(temptable)=="table" then
            else
                error("writetabletofile第二参数表不是表 请检查")
            end
            if path=="" or path==nil  then
                error("writetabletofile路径为空 请检查")
            else
                result=writeFile(path,temptable,"w") --将 table 内容存入文件,成功返回 true
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("writetabletofile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end


--读取指定路径文件全部内容到表中 成功返回表 读取失败返回false 不过要注意 返回值表只是传址
function  readReadLines(path)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result
            if path=="" or path==nil  then
                error("readReadLines路径为空 请检查")
            else
                result=readFile(path)
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("readReadLines") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end
--覆盖写入文件内容 失败的话返回false
function  writefile(path,str)
    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result
            local file
            if  str=="" or str==nil then
                error("writefile第二参数为空 请检查")
            end
            if path=="" or path==nil  then
                error("writefile路径为空 请检查")
            else
                result=writeFileString(path,str,"w")
                return result
--                file = io.open(path, "w")--w模式下如果文件不存在会自动创建
--                if file then
--                    file:write(str)
--                    io.close(file)
--                    return
--                else
--                    return false   
--                end
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("writefile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end

--追加写入文件内容
function  appendfile(path,str)

    return try{
        function ()
            --下面代码随便写 有可能抛出异常即可
            local result
            path=path or ""
            path=trim(path)
            if   str=="" or str==nil then
                error("appendfile第二参数为空 请检查")
            end
            if  path=="" or path==nil then
                error("appendfile路径为空字符串 请检查")
            else
                result=writeFileString(path,str,"a")
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("appendfile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end
posted @ 2019-08-20 10:12  点-滴  阅读(732)  评论(0编辑  收藏  举报