android設置時間[隨手記]

/**
	 * 設置系統時間
	 * 
	 * @param date
	 */
	public static final void setSystemTime(Date date) {
		try {
			Process process = Runtime.getRuntime().exec("su");
			DataOutputStream os = new DataOutputStream(process.getOutputStream());
			DateFormat format = new SimpleDateFormat("yyyyMMdd.hhmmss");
			String stringTime = format.format(date);
			String cmd = String.format("date -s %s\n", stringTime);
			os.writeBytes(cmd);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

最近在做一個應用程序,這個應用程序有根據GPS模塊設置系統時間的功能,於是在網上找了上述方法,但是這個方法在第一次設置時間的時候好使,但是在第二次設置系統時間的時候就報ANR錯誤(有時候不能指明是什麼錯誤,然後就莫名其妙的退出程序了),在程序調試過程中,由於代碼量太大,導致不知道是哪裏出現ANR錯誤,而且沒有錯誤定位

錯誤日誌:

07-27 01:04:47.050: E/InputDispatcher(1688): channel '411eb3b8 com.example.mactrotouch/com.example.mactrotouch.FullscreenActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
07-27 01:04:48.250: E/InputDispatcher(1688): Received spurious receive callback for unknown input channel.  fd=198, events=0x8


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章