5
我對android開發非常陌生,所以請原諒我的無知。 我需要能夠以15分鐘的間隔從遠程網頁讀取一些文本。網頁本身只包含一個沒有html標籤或格式的單詞。如果這是可能的,如果有人能指出我在正確的方向,我會很感激。在android中讀取遠程文本文件
感謝
我對android開發非常陌生,所以請原諒我的無知。 我需要能夠以15分鐘的間隔從遠程網頁讀取一些文本。網頁本身只包含一個沒有html標籤或格式的單詞。如果這是可能的,如果有人能指出我在正確的方向,我會很感激。在android中讀取遠程文本文件
感謝
當然,請嘗試以下
try {
// Create a URL for the desired page
URL url = new URL("yoursite.com/thefile.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
你可能想要把 「in.close()」 在最後{}條款,以確保它始終關閉
認沽上面的'TimerTask'的'run()'方法。然後使用'Timer.scheduleAtFixedRate()'。請參閱http://developer.android.com/reference/java/util/TimerTask.html和http://developer.android.com/reference/java/util/Timer.html – dorzey 2010-11-18 15:30:42
如果您的應用需要閱讀活動是背景或死亡,你會想用一個服務與報警,而不是:http://developer.android.com/reference/android/app/AlarmManager.html – 2010-11-18 15:42:59
你可能也想考慮分離出來一個不同的線程,因此在數據讀取時它不會鎖定您的用戶界面。 – phobos51594 2010-11-18 21:51:29