当前位置: 代码迷 >> PB >> Android快速开发之appBase——(四).详解com.snicesoft.Application和BaseActivity
  详细解决方案

Android快速开发之appBase——(四).详解com.snicesoft.Application和BaseActivity

热度:186   发布时间:2016-04-29 05:15:51.0
Android快速开发之appBase——(4).详解com.snicesoft.Application和BaseActivity

Android快速开发之appBase——(4).详解com.snicesoft.Application和BaseActivity

在 Android快速开发之appBase——(1).appBase介绍中使用过com.snicesoft.Application和BaseActivity,本篇则解开她们的面纱。


1. com.snicesoft.Application

1) 源码分析

package com.snicesoft;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.graphics.drawable.Drawable;import android.view.View;import com.lidroid.xutils.BitmapUtils;import com.lidroid.xutils.DbUtils;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.bitmap.BitmapDisplayConfig;import com.snicesoft.avlib.AVLib;import com.snicesoft.avlib.AVLib.LoadImg;import com.snicesoft.http.APIs;import com.snicesoft.http.Charset;import com.snicesoft.http.HttpReq;/** * 程序Application,作为应用管理配置 *  * @author zhe * */public class Application extends android.app.Application {    private HttpUtils httpUtils;    private BitmapUtils bitmapUtils;    private HashMap<String, DbUtils> dbHashMap = new HashMap<String, DbUtils>();    private HttpReq httpReq;    public HttpReq httpReq() {        if (httpReq == null)            httpReq = new HttpReq(httpUtils);        return httpReq;    }    public HttpUtils hu() {        return httpUtils;    }    public BitmapUtils bu() {        return bitmapUtils;    }    public DbUtils du(String dbName) {        if (dbHashMap.get(dbName) == null)            dbHashMap.put(dbName, DbUtils.create(getBaseContext(), dbName));        return dbHashMap.get(dbName);    }    @Override    public void onCreate() {        httpUtils = new HttpUtils();        httpUtils.configTimeout(APIs.Base.TIME_OUT);        httpUtils.configRequestThreadPoolSize(10);        httpUtils.configResponseTextCharset(Charset.UTF_8);        bitmapUtils = new BitmapUtils(getBaseContext());        AVLib.setLoadImg(new LoadImg() {            @SuppressWarnings("deprecation")            @Override            public void loadImg(View v, int loading, int fail, String uri) {                BitmapDisplayConfig config = new BitmapDisplayConfig();                if (loading != 0) {                    Drawable drawable = getResources().getDrawable(loading);                    config.setLoadingDrawable(drawable);                }                if (fail != 0) {                    Drawable drawable = getResources().getDrawable(fail);                    config.setLoadFailedDrawable(drawable);                }                bitmapUtils.display(v, uri, config);            }        });    }    private List<Activity> activities = new ArrayList<Activity>();    public void addActivity(Activity activity) {        activities.add(activity);    }    public void removeActivity(Activity activity) {        if (activities.contains(activity)) {            activities.remove(activity);        }    }    public void finishActivity(Class<?> activity) {        for (Activity act : activities) {            if (act.getClass() == activity) {                act.finish();                break;            }        }    }    public void exitApp() {        for (Activity activity : activities) {            activity.finish();        }        android.os.Process.killProcess(android.os.Process.myPid());        System.exit(0);    }}
  • List及相关的方法,是常用的App中管理activity的方法
  • exitApp() 是真正退出app的方法,只有将所有的activity全部finish,以及杀掉app的进程才能够真正退出一个应用。
  • HttpReq、BitmapUtils、DbUtils、HttpUtils 全部在application启动的时候配置好,并且提供对应方法获取,这样做的目的就是让整个APP只保留一个对象,放置资源浪费,任何只要能获取到Application的地方都可以获取对应的模块唯一对象进行操作。
  • hu():返回HttpUtils对象
  • bu():返回BitmapUtils对象
  • httpReq():返回HttpReq对象
  • du(String dbName):根据数据库名字返回DbUtils对象,提供多数据支持。

2) 怎么扩展

  • 可以继承com.snicesoft.Application实现自己的Application(推荐)
  • 也可以完全仿照com.snicesoft.Application自己实现Application

com.snicesoft.Application在最早的时候用到了static方法,由于static对于对象的引用控制不当,会导致很多问题,后来全部改掉。

2. BaseActivity

1) 源码分析

package com.snicesoft.base;import android.os.Bundle;import android.view.View;import com.snicesoft.Application;import com.snicesoft.avlib.base.AvFragment;import com.snicesoft.avlib.base.AvFragmentActivity;import com.snicesoft.avlib.rule.IData;import com.snicesoft.avlib.rule.IHolder;import com.snicesoft.util.FragmentUtil;public abstract class BaseActivity<H extends IHolder, D extends IData> extends        AvFragmentActivity<H, D> {    public Application getApp() {        return (Application) getApplication();    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getApp().addActivity(this);    }    protected AvFragment<?, ?, ?> curFragment;    public void openFragment(int id, AvFragment<?, ?, ?> fragment) {        if (curFragment != null && curFragment == fragment)            return;        FragmentUtil.openFragment(id, fragment, getSupportFragmentManager());        curFragment = fragment;    }    public void replaceFragment(int id, AvFragment<?, ?, ?> fragment,            boolean backStack) {        FragmentUtil.replaceFragment(id, fragment, getSupportFragmentManager(),                backStack);        curFragment = fragment;    }    @Override    public void onClick(View v) {        super.onClick(v);        if (curFragment != null)            curFragment.onClick(v);    }    @Override    protected void onDestroy() {        super.onDestroy();        getApp().removeActivity(this);    }}
  • BaseActivity扩展子AVLib中的AvFragmentActivity

2) 怎么扩展

  • 继承BaseActivity重写自己的BaseActivity并添加自己APP的通用业务(推荐)
  • 继承AVLib中的基础Activity
  相关解决方案