{"id":2919,"date":"2020-07-23T11:50:31","date_gmt":"2020-07-23T03:50:31","guid":{"rendered":"https:\/\/learn-house.idv.tw\/?p=2919"},"modified":"2020-07-23T11:50:31","modified_gmt":"2020-07-23T03:50:31","slug":"android%e7%94%a8%e7%a8%8b%e5%bc%8f%e5%9f%b7%e8%a1%8cadb-shell%e6%8c%87%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=2919","title":{"rendered":"[Android]\u7528\u7a0b\u5f0f\u57f7\u884cadb shell\u6307\u4ee4"},"content":{"rendered":"<p>\u958b\u767cAPP\u6709\u6642\u5019\u6703\u9047\u5230\u60f3\u76f4\u63a5\u7528shell\u6307\u4ee4\u7684\u65b9\u5f0f\u4f86\u57f7\u884c\u60f3\u8981\u7684\u7a0b\u5f0f\uff0c\u800c\u4e0d\u60f3\u70ba\u4e86\u4e00\u500b\u529f\u80fd\u5beb\u4e86\u4e00\u5806Java code<br \/>\n\u900f\u904e\u7a0b\u5f0f\u57f7\u884cshell\u6307\u4ee4\u6709\u5169\u4e2d\u65b9\u5f0f\uff0c\u4e00\u7a2e\u662f\u53ea\u8981\u57f7\u884c\uff0c\u4e0d\u7ba1\u57f7\u884c\u7d50\u679c\u7684\u56de\u8986\u5167\u5bb9\u6bd4\u8f03\u7c21\u55ae\uff0c\u5982\u4e0b\uff1a<br \/>\n[java]<br \/>\ntry {<br \/>\n    Runtime.getRuntime().exec(&quot;you shell command&quot;);<br \/>\n} catch (Exception e) {<br \/>\n    Log.e(&quot;Error: &quot;, e.getMessage());<br \/>\n}<br \/>\n[\/java]<br \/>\n<!--more--><br \/>\n\u53e6\u5916\u4e00\u7a2e\u5c31\u662f\u60f3\u77e5\u9053\u4e0b\u5b8c\u6307\u4ee4\u7684\u56de\u50b3\u7d50\u679c<br \/>\n[java]<br \/>\nprivate static void runShellCommand(String command) {<br \/>\n\tProcess process = null;<br \/>\n\tBufferedReader bufferedReader = null;<br \/>\n\tStringBuilder mShellCommandSB =new StringBuilder();<br \/>\n\tLog.d(TAG, &quot;runShellCommand :&quot; + command);<br \/>\n\tmShellCommandSB.delete(0, mShellCommandSB.length());<br \/>\n\tString[] cmd = new String[] { &quot;\/system\/bin\/sh&quot;, &quot;-c&quot;, command };<br \/>\n\ttry {<br \/>\n\t\tbyte b[] = new byte[1024];<br \/>\n\t\tprocess = Runtime.getRuntime().exec(cmd);<br \/>\n\t\tbufferedReader = new BufferedReader(new InputStreamReader(<br \/>\n\t\t\t\tprocess.getInputStream()));<br \/>\n\t\tString line;<\/p>\n<p>\t\twhile ((line = bufferedReader.readLine()) != null) {<br \/>\n\t\t\tmShellCommandSB.append(line);<br \/>\n\t\t}<br \/>\n\t\tLog.d(TAG, &quot;runShellCommand result : &quot; + mShellCommandSB.toString());<br \/>\n\t\tprocess.waitFor();<br \/>\n\t} catch (IOException e) {<br \/>\n\t\te.printStackTrace();<br \/>\n\t} catch (InterruptedException e) {<br \/>\n\t\te.printStackTrace();<br \/>\n\t} finally {<br \/>\n\t\tif (bufferedReader != null) {<br \/>\n\t\t\ttry {<br \/>\n\t\t\t\tbufferedReader.close();<br \/>\n\t\t\t} catch (IOException e) {<br \/>\n\t\t\t\t\/\/ TODO: handle exception<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\tif (process != null) {<br \/>\n\t\t\tprocess.destroy();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u958b\u767cAPP\u6709\u6642\u5019\u6703\u9047\u5230\u60f3\u76f4\u63a5\u7528shell\u6307\u4ee4\u7684\u65b9\u5f0f\u4f86\u57f7\u884c\u60f3\u8981\u7684\u7a0b\u5f0f\uff0c\u800c\u4e0d\u60f3\u70ba\u4e86\u4e00\u500b\u529f\u80fd\u5beb\u4e86\u4e00\u5806Java cod<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/learn-house.idv.tw\/?p=2919\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2919","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2919"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/2919\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}