重啟手機(jī)

測(cè)試調(diào)用 :

execCommand(new String[]{"am broadcast -a android.intent.action.BOOT_COMPLETED"}, true,true);

方法:

/**
 * execute shell commands
 *
 * @param commands        command array
 * @param isRoot          whether need to run with root
 * @param isNeedResultMsg whether need result msg
 * @return if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
 * {@link CommandResult#errorMsg} is null.
 * <p/>
 * <p/>
 * if {@link CommandResult#result} is -1, there maybe some excepiton.
 */
public static CommandResult execCommand(Object[] commands, boolean isRoot, boolean isNeedResultMsg) {
    int result = -1;
    if (commands == null || commands.length == 0) {
        return new CommandResult(result, null, null);
    }


    Process process = null;
    BufferedReader successResult = null;
    BufferedReader errorResult = null;
    StringBuilder successMsg = null;
    StringBuilder errorMsg = null;


    DataOutputStream os = null;
    try {
        process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
        os = new DataOutputStream(process.getOutputStream());
        for (Object command : commands) {
            String strCommand = String.valueOf(command);

            if (strCommand == null) {
                continue;
            }
            // donnot use os.writeBytes(strCommand), avoid chinese charset error
            os.write(strCommand.getBytes());
            os.writeBytes(COMMAND_LINE_END);
            os.flush();
        }
        os.writeBytes(COMMAND_EXIT);
        os.flush();


        result = process.waitFor();
        // get command result
        if (isNeedResultMsg) {
            successMsg = new StringBuilder();
            errorMsg = new StringBuilder();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                successMsg.append(s).append(COMMAND_RESULT_SEPARATOR);
            }
            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s).append(COMMAND_RESULT_SEPARATOR);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            if (successResult != null) {
                successResult.close();
            }
            if (errorResult != null) {
                errorResult.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (process != null) {
            process.destroy();
        }
    }
    return new CommandResult(result, successMsg == null ? null : successMsg.toString(),
            errorMsg == null ? null : errorMsg.toString());
}

CommandResult.java

public class CommandResult {
    public int result;
    public String successMsg;
    public String errorMsg;

    public CommandResult(int result, String successMsg, String errorMsg) {
        this.result = result;
        this.successMsg = successMsg;
        this.errorMsg = errorMsg;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末峡懈,一起剝皮案震驚了整個(gè)濱河市播揪,隨后出現(xiàn)的幾起案子立肘,更是在濱河造成了極大的恐慌彻况,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晰赞,死亡現(xiàn)場離奇詭異稼病,居然都是意外死亡选侨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門然走,熙熙樓的掌柜王于貴愁眉苦臉地迎上來援制,“玉大人,你說我怎么就攤上這事芍瑞〕柯兀” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵啄巧,是天一觀的道長。 經(jīng)常有香客問我掌栅,道長秩仆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任猾封,我火速辦了婚禮澄耍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晌缘。我一直安慰自己齐莲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布磷箕。 她就那樣靜靜地躺著选酗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪岳枷。 梳的紋絲不亂的頭發(fā)上芒填,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音空繁,去河邊找鬼殿衰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛盛泡,可吹牛的內(nèi)容都是我干的闷祥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼傲诵,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼凯砍!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起拴竹,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤果覆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后殖熟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體局待,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钳榨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片舰罚。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖薛耻,靈堂內(nèi)的尸體忽然破棺而出营罢,到底是詐尸還是另有隱情,我是刑警寧澤饼齿,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布饲漾,位于F島的核電站,受9級(jí)特大地震影響缕溉,放射性物質(zhì)發(fā)生泄漏考传。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一证鸥、第九天 我趴在偏房一處隱蔽的房頂上張望僚楞。 院中可真熱鬧,春花似錦枉层、人聲如沸泉褐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膜赃。三九已至,卻和暖如春揉忘,著一層夾襖步出監(jiān)牢的瞬間财剖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國打工癌淮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留躺坟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓乳蓄,卻偏偏與公主長得像咪橙,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子虚倒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容