分享

自己整合优化的一个Android框架

 WindySky 2016-05-05
摘要 最近网上成熟的Android框架挺多的,但是自己感觉用起来还是有点太麻烦,所以就自己整理了一套Android快速开发框架,大神勿喷

我封装的这个框架暂且就叫它Wonder框架吧, Wonder惊奇的意思,意思是让人眼前一亮。改变了原先我们喜欢重复复制粘贴代码的坏习惯,浪费了时间的同时还不好管理代码。

废话不说,咱们先看看简单架构:


AWonder企业框架

 

 

AWonder是一个androidsqlite orm 和 ioc 框架。同时封装了android中的http,多任务断点文件下载和一些常用的封装。

 

数据解析:

使用AsyncHttpClient进行Json解析:

1>、第一步:我们需要写个内部类或者是普通的专门用于网络请求的类,我喜欢将所有的网络请求类放在一个包中,这个看个人爱好。

1
2
3
public class HttpClientFile extends HttpRestClient implements HttpRestClient.BaseControlInterface{
}

2>、第二步:实现httpRestClient中的所有方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      public HttpClientFile(Context context) {
            super(context);
        }
                /**
         * 请求服务器的IP
         */
        @Override
        public String url() {
            return null;
        }
        /**
         * 文件上传的参数配置
         */
        @Override
        public SimpleMultipartEntity setMultipartEntityMessage() {
            return null;
        }
        /**
         * 服务器连接失败操作
         */
        @Override
        public void onFailureMessage(Throwable error, String content) {
         
        }
        /**
         * 网络请求成功之后进行的操作
         */
        @Override
        public void onSucceMessage(int statusCode, String content) {
            // TODO Auto-generated method stub
             
        }
        /**
         * 请求服务器参数的配置
         */
        @Override
        public RequestParams setParamsMessage() {
            return null;
        }
    }

需要注意的是:我通过接口的形式封装的这个类有一个地方需要注意:如果你只是请求网络返回json或者是xml的话那你只需要在url方法中填写你的ip,然后在setParamsMessage方法中将请求参数拼接就行,然后在返回的onSuccessMessage或者是onFailureMessage方法中进行相关的操作。

如果你需要上传文件的话,那你需要在“setMultipartEntityMessage”方法中进行参数拼接,并且在此处添加文件流。这样就可以实现文件传输了,具体有例子可以证明:

使用例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class HttpClientFile extends HttpRestClient implements HttpRestClient.BaseControlInterface{
        public HttpClientFile(Context context) {
            super(context);
        }
        @Override
        public String url() {
            //请求服务器的IP
            return null;
        }
        /**
         * 文件上传的参数配置
         */
        @Override
        public SimpleMultipartEntity setMultipartEntityMessage() {
            //文件上传的参数配置
            SimpleMultipartEntity simpleentity = new SimpleMultipartEntity();
             
            //在表单中,增加第一个input type='file'元素,name=‘uploadedfile’
            File file2 = new File(srcPath2);
            try {
                simpleentity.addPart("img", file2.getName(), new FileInputStream(file2), true);
            catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return simpleentity;
        }
        /**
         * 服务器连接失败操作
         */
        @Override
        public void onFailureMessage(Throwable error, String content) {
            //服务器连接失败操作
        }
        /**
         * 网络请求成功之后进行的操作
         */
        @Override
        public void onSucceMessage(int statusCode, String content) {
            // TODO Auto-generated method stub
             
        }
        /**
         * 请求服务器参数的配置
         */
        @Override
        public RequestParams setParamsMessage() {
            RequestParams params = new RequestParams();
            params.put("mac""00:d3:4b:10:00:30");
            params.put("sn""00:d3:4b:10:00:30");
            params.put("type""1");
            params.put("key""5d10372f217d59a4844bb6bb12be6de1");
            return params;
        }
         
    }

使用XmlUtil的PULL进行XML解析:

1>、第一步:XmlUtil xmlUtil= new XmlUtil();

2>、第二步:实现xmlUtil中的方法,包括(xmlUtil.getXmlList(); xmlUtil.getXmlObject())

使用例子:

//实例化一个List<JavaBean>

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>  
<person personid="0000000" id="1010" >   
    <people people id="002">   
     <name>张三</name>   
     <tel>12345</tel>   
   </people>   
       
   <people people id="002">   
      <name>李四</name>   
      <tel>678910</tel>   
  </people>   
   </person>


//实例化一个List<JavaBean>

1
2
3
4
5
6
List<People> mList = new ArrayList<People>();
XmlUtil xmlUtil = new XmlUtil();
People people = new People();
     mList = xmlUtil.getXmlList(url, people, "people");                 1)
  
   mList = xmlUtil.getXmlObject(url, people);

                        2)

 

 

使用AWonder对于图片下载处理:

 

AWonder执行的是缓存,然后SD卡,最后网络

 

下载的顺序。

 

使用例子方法一  

1
2
3
4
5
6
7
8
9
10
11
12
/**
         * 需要在此处添加
         */
        BitmapUtils  bitmapUtils = BitmapHelp.getBitmapUtils(getApplicationContext(), Environment.getExternalStorageDirectory() + "/ai");
         bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
         bitmapUtils.configDefaultLoadFailedImage(R.drawable.test_list_view_bg);
         bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
//         bitmapUtils.configMemoryCacheEnabled(false);    //是否设置内存缓存
//         bitmapUtils.configDiskCacheEnabled(false);     //是否设置SD卡文件缓存
         bitmapUtils.configDefaultAutoRotation(true);    //设置是否旋转


最后调用

   

1
bitmapUtils.display(imageView, url, new CustomBitmapLoadCallBack(holder));


 

方法二

ABitmap bitmap = ABitmap.create(getApplicationContext(), diskCachePath);

bitmap.display(imageViewurl);



AWOnder有对缓存处理的封装:

 

通过CacheG 和 CahceP中的getCache()putCache()方法,您只需要调用一句话就能实现缓存的存取。

 

调用的方法:

@Override

public void putCacheInterG(Context context, String spName, String key,

int value) {

// TODO Auto-generated method stub

super.putCacheInterG(context, spName, key, value);

}

 

@Override

public void putCacheStringG(Context context, String spName, String key,

String value) {

// TODO Auto-generated method stub

super.putCacheStringG(context, spName, key, value);

}

@Override

public void putCacheLongG(Context context, String spName, String key,

long value) {

// TODO Auto-generated method stub

super.putCacheLongG(context, spName, key, value);

}

@Override

public void putCacheBooleanG(Context context, String spName, String key,

Boolean value) {

// TODO Auto-generated method stub

super.putCacheBooleanG(context, spName, key, value);

}

 

注:getacheInteger取值时默认为0

getCacheString取值时默认为null

getCacheBoolean取值时默认为true

 

AWonder通过downloadManager进行多任务下载文件:

 

Downloadmanager下载文件:

   

DownloadManager downloadManager = DownloadManager.getDownloadManager(getApplicationContext());

downloadManager.configDownloadPath("FilePath");   //配置下载完的文件存储的位置

downloadManager.addHandler(url);    //添加要下载文件的URL

downloadManager.setDownLoadCallback(new DownLoadCallback() {

@Override 

public void onSuccess(String url) {

Toast.makeText(DownLoadDemoActivity.this"数据下载完成,请查看!", Toast.LENGTH_SHORT).show();

}

@Override

public void onLoading(String url, long totalSize, long currentSize,

long speed) {

super.onLoading(url, totalSize, currentSize, speed); 

Toast.makeText(DownLoadDemoActivity.this"正在下载数据.....", Toast.LENGTH_SHORT).show();

}

});

 

AWonder通过FileUtils进行SD卡文件操作:

/**

 * 写文本文件

 * 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下

 * @param context

 * @param msg

 */

FileUtils.write(Context context, String fileName, String content))    //写文件

/**

 * 读取文本文件

 * @param context

 * @param fileName   //文件名

 * @return

*/

FileUtils.read(Context context, String fileName )             //读文件

/**

 * @param folderPath   //文件夹名称

 * @param fileName   //文件名

 * @return

 */

FileUtils.createFile(String folderPath, String fileName  )             //读文件

 

/**

 * 根据文件绝对路径获取文件名

 * @param filePath

 * @return

 */

FileUtils.getFileName( String filePath )

/**

 * 检查文件是否存在

 * @param name

 * @return

 */

FileUtils.checkFileExists(String name) 

/**

 * 计算SD卡的剩余空间

 * @return 返回-1,说明没有安装sd卡

 */

FileUtils.getFreeDiskSpace() 

/**

 * 检查是否安装SD卡

 * @return

 */

FileUtils.checkSaveLocationExists() 

/**

 * 删除文件

 * @param fileName

 * @return

 */

FileUtils.deleteFile(String fileName) 

 

AWonder通过CheckNet进行判断网络状况:

 

/**

 * 检测网络是否连接(包括3G和Wifi网)

  * @return

  */

Boolean bool = CheckNet.checkNet(Context context);

/**判断WIFI网络是否打开

 * @param activity

 * @return

 */

Boolean bool = CheckNet.isWifiActive(Activity activity)

/**

* 判断网络是否打开

 * @param activity

 * @return

 */

Boolean bool = CheckNet.isWifiActive(Context context)

/**

 * 判断是否有网络连接,如果没有联网就弹出Dialog并提示

 * @param activity

 */

Boolean bool = CheckNet.CheckNetworkState(Activity activity) 

 

 

AWonder通过CyptoThreeDS进行3DS数据加解密:

// 解密数据

CyptoThreeDS.decrypt(String message, String key)

//加密数据

CyptoThreeDS.encrypt(String message, String key)

 

AWonder通过CyptoUtils进行MD5DS数据加解密:

/**

 * MD5加密

 * @param content

 */

CyptoUtils.getMD5(String content) 

 /**

     * DES算法,加密

     * @param data 待加密字符串

     * @param key  加密私钥,长度不能够小于8位

     * @return 加密后的字节数组,一般结合Base64编码使用

     * @throws InvalidAlgorithmParameterException 

     * @throws Exception 

     */

CyptoUtils.encode(String key,String data) 

 

 /**

     * DES算法,解密

     * @param data 待解密字符串

     * @param key  解密私钥,长度不能够小于8位

     * @return 解密后的字节数组

     * @throws Exception 异常

     */

CyptoUtils.decode(String key,String data)

 

AWonder通过PhoneSystemInfo获取手机信息:

/**

 * 获取手机ip start

 */

public static String getLocalIpAddress() 

 

/**

 *  获取手机ip method-2

 * @return

 */

public static String getLocalIpAddress2() 

 

/**

 * mac地址 start

 */

public static String getLocalMacAddress() 

 

/**

 * 获取Android手机中SD卡存储信息 获取剩余空间

 * @param Exception      ==   SD卡不存在

 * @return

 */

 

 

public static long getSDCardInfo(String Exception) 

/**

 * 获取Android手机中SD卡存储信息 获取剩余空间

 * @param Exception      ==   SD卡不存在

 * @return 

 */

public static long getSDCardInfo1(String Exception) 

 

 

/**

 * 获取手机可用内存

 * @return 

 * 

 */

public static String getSystemMemory() {

}

/**

 * 获取手机总内存

 * @return 

 * 

 */

public static String getSystemMemory1() {

}

 

/**

 * 获取手机CPU信息

 * @param tag    ------- tag = 1(获取cpu型号)                ----------tag = 2(获取cpu型号)

 * @return

 */

 

private static String getCpuInfo(String tag) 

 

/**

 * 手机IMEI

 */

private static String getImei() 

/**

 * 获取Wifi的MAC地址,需要开启才能获取

 * @return

 */

public static String getLocalWifiMacAddressT() 

 

/**

 * 获取Wifi的MAC地址,只需要开启过以后就都能获取

 * @return

 */

public static String getLocalNetAddressMAC() 

 

/**

 * 获取以太网MAC地址

 * @return

 */

public static String getLocalIpAddressMAC() 

AWonder通过Intent实现页面跳转:

/**

  * 跳转到下一个Activity

  * @param 当前Activity

  * @param 要跳转的Activity

  */

 public static void Go(Context context, Class<?> cls) 

 

 /**

  * @param context

  * @param cls

  * @param bundle用于上一个activity向下一个activity传参

  */

 public static void Go(Context context, Class<?> cls, Bundle bundle) 

 

 public static void Go(Context context, Class<?> cls,

   HashMap<String, Serializable> map) 

 

AWonder通过TipDialogTipToast实现重写DialogToast

Dialog:

 

tipDialog = new TipDialog(TipsActivity.this, R.style.Theme_dialog, R.layout.activity_dialog, 400, 200, 1111);

 

 

TOP:代表在最顶端

BOTTOM:代表在最下端

CENTER:代表在最中间

 

 

tipDialog.show();   //显示

tipDialog.dismiss();   //消失

 

Toast:

 

TipToast.showToast(R.layout.toast_main, getApplicationContext(), 1111, 1101);

 

AWonder中还封装了一些常用的类:比如:LogU.java(用于Log打印),电话和短信的封装(intecept/phone../phonemsg../sms)等

最后是jar包和源码


jar包:http://download.csdn.net/detail/wangkai1101/9105251

源码:http://download.csdn.net/detail/wangkai1101/9105257

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多