{"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<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntry {\r\n    Runtime.getRuntime().exec(&quot;you shell command&quot;);\r\n} catch (Exception e) {\r\n    Log.e(&quot;Error: &quot;, e.getMessage());\r\n}\r\n<\/pre>\n<p><!--more--><br \/>\n\u53e6\u5916\u4e00\u7a2e\u5c31\u662f\u60f3\u77e5\u9053\u4e0b\u5b8c\u6307\u4ee4\u7684\u56de\u50b3\u7d50\u679c<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate static void runShellCommand(String command) {\r\n\tProcess process = null;\r\n\tBufferedReader bufferedReader = null;\r\n\tStringBuilder mShellCommandSB =new StringBuilder();\r\n\tLog.d(TAG, &quot;runShellCommand :&quot; + command);\r\n\tmShellCommandSB.delete(0, mShellCommandSB.length());\r\n\tString&#x5B;] cmd = new String&#x5B;] { &quot;\/system\/bin\/sh&quot;, &quot;-c&quot;, command };\r\n\ttry {\r\n\t\tbyte b&#x5B;] = new byte&#x5B;1024];\r\n\t\tprocess = Runtime.getRuntime().exec(cmd);\r\n\t\tbufferedReader = new BufferedReader(new InputStreamReader(\r\n\t\t\t\tprocess.getInputStream()));\r\n\t\tString line;\r\n\t\r\n\t\twhile ((line = bufferedReader.readLine()) != null) {\r\n\t\t\tmShellCommandSB.append(line);\r\n\t\t}\r\n\t\tLog.d(TAG, &quot;runShellCommand result : &quot; + mShellCommandSB.toString());\r\n\t\tprocess.waitFor();\r\n\t} catch (IOException e) {\r\n\t\te.printStackTrace();\r\n\t} catch (InterruptedException e) {\r\n\t\te.printStackTrace();\r\n\t} finally {\r\n\t\tif (bufferedReader != null) {\r\n\t\t\ttry {\r\n\t\t\t\tbufferedReader.close();\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\t\/\/ TODO: handle exception\r\n\t\t\t}\r\n\t\t}\r\n\t\r\n\t\tif (process != null) {\r\n\t\t\tprocess.destroy();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\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 [&hellip;]<\/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"}],"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}]}}