0
我有一個簡單的Android應用程序。我通過使用JSOUP從網站獲取HTML元素(wikipedia上的文章計數)。我收到文章按鈕點擊計數和RefreshBtn()
展現在一個TextView tv1
,如下圖所示:Android定期檢查網站更新(JSOUP)
public class MainActivity extends ActionBarActivity {
String URL = "https://en.wikipedia.org";
Element article;
TextView tv1;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.tv1);
}
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect(URL).userAgent("Mozilla").get();
article = doc.select("div#articlecount > a").first();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if(article == null) tv1.setText("null!");
else tv1.setText(article.text() + " articles found!");
mProgressDialog.dismiss();
}
}
public void RefreshBtn(View v) {
new FetchWebsiteData().execute();
}
...
}
我想文章定期計算(例如每2小時)。那麼,如果有變化,我可以創建推送通知。做這個的最好方式是什麼?我需要一些建議。謝謝。
你確定第二種方式(帶螺紋)是週期性的?你不應該在裏面加一個循環嗎? – TDG
你說的對!謝謝 –