【发布时间】:2013-03-10 06:35:26
【问题描述】:
我开发了一个安卓应用程序,它使用Yahoo! Weather API 获取天气信息,因为Yahoo! Weather API 允许每小时仅请求10 to 20 次服务。当我在它们之间来回切换时,我的应用程序有两个活动我的应用程序一次又一次地请求天气服务我的代码是:
@Override
protected void onResume() {
// TODO Auto-generated method stub
String nameOfCity= pref.getString("cityname", cityNameInput.getText().toString());
getWeather(null, nameOfCity);// this the AsyncTask which is used to request Yahoo! Weather service.
cityNameInput.setText(nameOfCity);
super.onResume();
Log.d("My_TAG", "in onResume()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
editor = pref.edit();
if(cityNameInput.getText().toString().equals("")){
cityNameInput.setText(yahooWeatherInfo.getLocationCity());
}else{
editor.putString("cityname", cityNameInput.getText().toString());
}
editor.commit();
super.onPause();
Log.d("MY_TAG", "in onPause()");
}
现在请告诉我如何限制它不要在活动之间一次又一次地请求Yahoo! Weather Service。
【问题讨论】:
-
考虑让您的数据请求成为 Android 服务。
-
@MorrisonChang 我该怎么做请给我一些提示?
标签: android android-activity activity-lifecycle yahoo-weather-api