android 系統會爲每一個程序創建一個Application對象,
1.Application是一個全局對象,生命週期最長,從程序的開始到結束
2.Application 是一個單例類,只能有一個對象
3.Application 的入口onCreate()方法先於其他任何組建的入口 ,率先執行。
4.Application可以用來數據加載緩存。
可以自定義Application類
public class Application extends android.app.Application implements Iapplicationdata{ public static ArrayList<EventHandler> mListeners = new ArrayList<EventHandler>(); private static String NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; private static final int CITY_LIST_SCUESS = 0; private static final String FORMAT = "^[a-z,A-Z].*$"; private CityDB mCityDB; private Map<String, Integer> mWeatherIcon;// 天氣圖標 private Map<String, Integer> mWidgetWeatherIcon;// 插件天氣圖標 private List<City> mCityList; // 首字母集 private List<String> mSections; // 根據首字母存放數據 private Map<String, List<City>> mMap; // 首字母位置集 private List<Integer> mPositions; // 首字母對應的位置 private Map<String, Integer> mIndexer; private boolean isCityListComplite; private LocationClient mLocationClient = null; private SharePreferenceUtil mSpUtil; private Weatherinfo mCurWeatherinfo; private SimpleWeatherinfo mCurSimpleWeatherinfo; private Pm2d5 mCurPm2d5; public static int mNetWorkState; private static Application mApplication; public static synchronized Application getInstance(){ return mApplication; } @Override public void onCreate() { super.onCreate(); initData(); } public void initData() { mApplication = this;//直接用this初始化對象 initWeatherIconMap(); initWidgetWeather(); } private void initWidgetWeather() { mWidgetWeatherIcon = new HashMap<String, Integer>(); mWidgetWeatherIcon.put("暴雪", R.drawable.w17); mWidgetWeatherIcon.put("暴雨", R.drawable.w10); mWidgetWeatherIcon.put("大暴雨", R.drawable.w10); mWidgetWeatherIcon.put("大雪", R.drawable.w16); mWidgetWeatherIcon.put("大雨", R.drawable.w9); mWidgetWeatherIcon.put("多雲", R.drawable.w1); mWidgetWeatherIcon.put("雷陣雨", R.drawable.w4); mWidgetWeatherIcon.put("雷陣雨冰雹", R.drawable.w19); mWidgetWeatherIcon.put("晴", R.drawable.w0); mWidgetWeatherIcon.put("沙塵暴", R.drawable.w20); mWidgetWeatherIcon.put("特大暴雨", R.drawable.w10); mWidgetWeatherIcon.put("霧", R.drawable.w18); mWidgetWeatherIcon.put("小雪", R.drawable.w14); mWidgetWeatherIcon.put("小雨", R.drawable.w7); mWidgetWeatherIcon.put("陰", R.drawable.w2); mWidgetWeatherIcon.put("雨夾雪", R.drawable.w6); mWidgetWeatherIcon.put("陣雪", R.drawable.w13); mWidgetWeatherIcon.put("陣雨", R.drawable.w3); mWidgetWeatherIcon.put("中雪", R.drawable.w15); mWidgetWeatherIcon.put("中雨", R.drawable.w8); } private void initWeatherIconMap() { mWeatherIcon = new HashMap<String, Integer>(); mWeatherIcon.put("暴雪", R.drawable.biz_plugin_weather_baoxue); mWeatherIcon.put("暴雨", R.drawable.biz_plugin_weather_baoyu); mWeatherIcon.put("大暴雨", R.drawable.biz_plugin_weather_dabaoyu); mWeatherIcon.put("大雪", R.drawable.biz_plugin_weather_daxue); mWeatherIcon.put("大雨", R.drawable.biz_plugin_weather_dayu); mWeatherIcon.put("多雲", R.drawable.biz_plugin_weather_duoyun); mWeatherIcon.put("雷陣雨", R.drawable.biz_plugin_weather_leizhenyu); mWeatherIcon.put("雷陣雨冰雹", R.drawable.biz_plugin_weather_leizhenyubingbao); mWeatherIcon.put("晴", R.drawable.biz_plugin_weather_qing); mWeatherIcon.put("沙塵暴", R.drawable.biz_plugin_weather_shachenbao); mWeatherIcon.put("特大暴雨", R.drawable.biz_plugin_weather_tedabaoyu); mWeatherIcon.put("霧", R.drawable.biz_plugin_weather_wu); mWeatherIcon.put("小雪", R.drawable.biz_plugin_weather_xiaoxue); mWeatherIcon.put("小雨", R.drawable.biz_plugin_weather_xiaoyu); mWeatherIcon.put("陰", R.drawable.biz_plugin_weather_yin); mWeatherIcon.put("雨夾雪", R.drawable.biz_plugin_weather_yujiaxue); mWeatherIcon.put("陣雪", R.drawable.biz_plugin_weather_zhenxue); mWeatherIcon.put("陣雨", R.drawable.biz_plugin_weather_zhenyu); mWeatherIcon.put("中雪", R.drawable.biz_plugin_weather_zhongxue); mWeatherIcon.put("中雨", R.drawable.biz_plugin_weather_zhongyu); System.out.println("initWeatherIconMap"); } public int getWeatherIcon(String climate) { int weatherRes = R.drawable.na; if (TextUtils.isEmpty(climate)) return weatherRes; String[] strs = { "晴", "晴" }; if (climate.contains("轉")) {// 天氣帶轉字,取前面那部分 strs = climate.split("轉"); climate = strs[0]; if (climate.contains("到")) {// 如果轉字前面那部分帶到字,則取它的後部分 strs = climate.split("到"); climate = strs[1]; } } if (mWidgetWeatherIcon.containsKey(climate)) { weatherRes = mWidgetWeatherIcon.get(climate); } return weatherRes; } @Override public Map<String, Integer> getWeatherIcon() { return mWeatherIcon; } @Override public Map<String, Integer> getWidgetWeatherIcon() { return mWidgetWeatherIcon; } }
在AndroidManifest.xml 文件中做出修改::注意
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.daily.pengshu.happyweather"> <uses-permission android:name="android.permission.INTERNET"/> <application android:name=".model.Application" android:allowBackup="true" android:icon="@drawable/logo" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".view.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>