android Application 类的使用

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>

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值