我开发了一个使用 Yahoo! Weather API
用于获取天气信息,如 Yahoo!天气 API
仅允许每小时请求服务 10 到 20
次。当我在它们之间来回切换时,我的应用程序有两个 Activity 我的应用程序一次又一次地请求天气服务我的代码是:
@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!天气服务
一次又一次地在 Activity 之间切换。
最佳答案
我看了你的代码。
您正在 onResume()
中调用 getWeather(null, nameOfCity);
。当您的 Activity 再次恢复时,将调用此 onResume()
方法。
例如:A ---> B then Press back from B activity 然后 A activity onResume()
方法将被调用并向服务器发出请求。所以在 onCreate(bundle)
中调用此方法 `getWeather(null, nameOfCity);
。创建 Activity 时仅调用一次。
关于android - 代码一次又一次地请求雅虎天气服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15319624/