首页 > 代码库 > ThinkAndroid 1.1版本的简介及基本使用方法

ThinkAndroid 1.1版本的简介及基本使用方法

  • ThinkAndroid 交流平台
  • ThinkAndroid简介
  • 目前ThinkAndroid主要有以下模块:
  • 使用ThinkAndroid快速开发框架需要有以下权限:
  • Activity必须继承TAActivity
  • ThinkAndroid使用方法:
  • MVC模块
  • ioc模块使用方法:
  • 数据库模块
  • Http模块使用方法:
  • 异步get方法
  • 同步get方法
  • 使用http下载文件:
  • 缓存模块 方法
  • 打印模块使用方法
  • 下载器模块
  • 下载器后台运行
  • 配置器模块:
  • 网络状态检测模块: 
  • 关于作者(白猫)

ThinkAndroid 交流平台

  • QQ群:169415162
  • 网址:http://www.thinkandroid.cn

ThinkAndroid简介

ThinkAndroid是一个免费的开源的、简易的、遵循Apache2开源协议发布的Android开发框架,其开发宗旨是简单、快速的进行Android应用程序的开发,包含Android mvc、简易sqlite orm、ioc模块、封装Android httpclitent的http模块,具有快速构建文件缓存功能,无需考虑缓存文件的格式,都可以非常轻松的实现缓存,它还基于文件缓存模块实现了图片缓存功能,在android中加载的图片的时候,对oom的问题,和对加载图片错位的问题都轻易解决。他还包括了一个手机开发中经常应用的实用工具类,如日志管理,配置文件管理,android下载器模块,网络切换检测等等工具。

目前ThinkAndroid主要有以下模块:

  • MVC模块:实现视图与模型的分离。

  • ioc模块:android中的ioc模块,完全注解方式就可以进行UI绑定、res中的资源的读取、以及对象的初始化。

  • 数据库模块:android中的orm框架,使用了线程池对sqlite进行操作。

  • http模块:通过httpclient进行封装http数据请求,支持异步及同步方式加载。

  • 缓存模块:通过简单的配置及设计可以很好的实现缓存,对缓存可以随意的配置

  • 图片缓存模块:imageview加载图片的时候无需考虑图片加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象。

  • 配置器模块:可以对简易的实现配对配置的操作,目前配置文件可以支持Preference、Properties对配置进行存取。

  • 日志打印模块:可以较快的轻易的是实现日志打印,支持日志打印的扩展,目前支持对sdcard写入本地打印、以及控制台打印

  • 下载器模块:可以简单的实现多线程下载、后台下载、断点续传、对下载进行控制、如开始、暂停、删除等等。

  • 网络状态检测模块:当网络状态改变时,对其进行检测。

使用ThinkAndroid快速开发框架需要有以下权限:

<!-- 访问互联网权限 根据需要添加-->  <uses-permission android:name="android.permission.INTERNET" />  <!-- SDCARD读写权限 根据需要添加 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 网络状态检测权限  根据需要添加--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:name="com.ta.TAApplication" > application必须配置以上格式

Activity必须继承TAActivity

需要使用 android-support-v4.jar支持

ThinkAndroid使用方法:

关于ThinkAndroid的更多介绍,请点击这里

MVC模块

  • MVC模块:实现视图与模型的分离。
 
view source
 
print?
01 getTAApplication().registerCommand(R.string.comand,
02                 TAIdentityCommand.class);
03         getTAApplication().registerCommand(R.string.comand,
04                 TAIdentityCommand.class);
05         TALogger.addLogger(new TAPrintToFileLogger());
06         TARequest request = new TARequest();
07         doCommand(R.string.comand, request, new TAIResponseListener()
08         {
09  
10             @Override
11             public void onStart(TAResponse response)
12             {
13                 // TODO Auto-generated method stub
14  
15             }
16  
17             @Override
18             public void onSuccess(TAResponse response)
19             {
20                 // TODO Auto-generated method stub
21  
22             }
23  
24             @Override
25             public void onRuning(TAResponse response)
26             {
27                 // TODO Auto-generated method stub
28  
29             }
30  
31             @Override
32             public void onFailure(TAResponse response)
33             {
34                 // TODO Auto-generated method stub
35  
36             }
37  
38         }, false, true, true);
39  
40  
41  
42  
43 package com.ta.mvc.command;
44  
45 import com.ta.mvc.common.TAIResponseListener;
46 import com.ta.mvc.common.TARequest;
47 import com.ta.mvc.common.TAResponse;
48  
49 public class TAIdentityCommand extends TACommand
50 {
51   @Override
52     protected void executeCommand()
53     {
54         // TODO Auto-generated method stub
55         TARequest request = getRequest();
56         TAResponse response = new TAResponse();
57         response.setTag(request.getTag());
58         response.setData(request.getData());
59         response.setActivityKey((String) request.getActivityKey());
60         response.setActivityKeyResID(request.getActivityKeyResID());
61         setResponse(response);
62         notifyListener(true);
63     }
64  
65     protected void notifyListener(boolean success)
66     {
67         TAIResponseListener responseListener = getResponseListener();
68         if (responseListener != null)
69         {
70             onComandUpdate(command_success);
71         }
72     }
73 }

ioc模块使用方法:

  • 完全注解方式就可以进行UI绑定、res中的资源的读取、以及对象的初始化。
view source
 
print?
01 public class ThinkAndroidDemoActivity extends TAActivity {
02  
03  
04     @TAInject
05     Entity entity; //目前只能对无参构造函数进行初始化
06     @TAInjectResource(R.string.app_name)
07     String appNameString;
08     @TAInjectResource(R.attr.test)
09     int[] test;
10     @TAInjectView(R.id.add);
11     Button addButton;
12 }
 

数据库模块

  • android中的orm框架,使用了线程池对sqlite进行操作。
public class ThinkAndroidDemoActivity extends TAActivity { TASQLiteDatabasePool sqlitePool = getTAApplication() .getSQLiteDatabasePool(); TASQLiteDatabase sqliteDatabase=sqlitePool.getSQLiteDatabase(); //使用中 sqliteDatabase.insert(entity); sqlitePool.returnSQLiteDatabase(sqliteDatabase); }

Http模块使用方法:

异步get方法

 
view source
 
print?
01 AsyncHttpClient client = new AsyncHttpClient();
02        client.get("http://www.thinkandroid.cn/", new AsyncHttpResponseHandler()
03        {
04            @Override
05            public void onSuccess(String content)
06            {
07                // TODO Auto-generated method stub
08                super.onSuccess(content);
09                TALogger.d(LoginActivity.this, content);
10            }
11  
12            @Override
13            public void onStart()
14            {
15                // TODO Auto-generated method stub
16                super.onStart();
17            }
18  
19            @Override
20            public void onFailure(Throwable error)
21            {
22                // TODO Auto-generated method stub
23                super.onFailure(error);
24            }
25  
26            @Override
27            public void onFinish()
28            {
29                // TODO Auto-generated method stub
30                super.onFinish();
31            }
32  
33        });

同步get方法

 TASyncHttpClient client = new TASyncHttpClient();     client.get("http://www.thinkandroid.cn/", new AsyncHttpResponseHandler()         {             @Override             public void onSuccess(String content)             {                 // TODO Auto-generated method stub                 super.onSuccess(content);                 TALogger.d(LoginActivity.this, content);             }

            @Override             public void onStart()             {                 // TODO Auto-generated method stub                 super.onStart();             }

            @Override             public void onFailure(Throwable error)             {                 // TODO Auto-generated method stub                 super.onFailure(error);             }

            @Override             public void onFinish()             {                 // TODO Auto-generated method stub                 super.onFinish();             }

        });

文件上传到服务器,服务器如何接收,请查看这里

view source
 
print?
01 RequestParams params = new RequestParams();
02       params.put("username", "white_cat");
03           params.put("password", "123456");
04           params.put("email", "2640017581@qq.com");
05           params.put("profile_picture", new File("/mnt/sdcard/testpic.jpg")); // 上传文件
06           params.put("profile_picture2", inputStream); // 上传数据流
07           params.put("profile_picture3", new ByteArrayInputStream(bytes)); // 提交字节流
08         client.post("http://www.thinkandroid.cn/", new AsyncHttpResponseHandler()
09         {
10             @Override
11             public void onSuccess(String content)
12             {
13                 // TODO Auto-generated method stub
14                 super.onSuccess(content);
15                 TALogger.d(LoginActivity.this, content);
16             }
17  
18             @Override
19             public void onStart()
20             {
21                 // TODO Auto-generated method stub
22                 super.onStart();
23             }
24  
25             @Override
26             public void onFailure(Throwable error)
27             {
28                 // TODO Auto-generated method stub
29                 super.onFailure(error);
30             }
31  
32             @Override
33             public void onFinish()
34             {
35                 // TODO Auto-generated method stub
36                 super.onFinish();
37             }
38  
39         });
40     }
 

使用http下载文件:

  • 支持断点续传,随时停止下载任务 或者 开始任务
view source
 
print?
01 AsyncHttpClient syncHttpClient = new AsyncHttpClient();
02        FileHttpResponseHandler fHandler = new FileHttpResponseHandler(
03                     TAExternalOverFroyoUtils.getDiskCacheDir(TestActivity.this,
04                      "sdfsdfsdf").getAbsolutePath())
05            {
06  
07  
08                      @Override
09                      public void onProgress(String speed, String progress) {
10                          //TODO Auto-generated method stub
11                          super.onProgress(speed, progress);
12                      TALogger.v(TestActivity.this, progress + "--------" + speed); }
13  
14                      @Override
15                      public void onFailure(Throwable error) {
16                          // TODOAuto-generated method stub
17                          super.onFailure(error); }
18  
19                  @Override
20                  public void onSuccess(byte[] binaryData) {
21                      // TODOAuto-generated method stub
22                      super.onSuccess(binaryData);
23                  TALogger.d(TestActivity.this, "kaishi8了"); } };
24          syncHttpClient .download(
25                 "http://static.qiyi.com/ext/common/iQIYI/QIYImedia_4_01.exe",
26                  fHandler);
27          //停止
28         fHandler.setInterrupt(interrupt);
 
  • imageview加载图片的时候无需考虑图片加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象。
view source
 
print?
01 package com.test;
02 import com.ta.TAApplication;
03 import com.ta.util.bitmap.TABitmapCacheWork;
04 import com.ta.util.bitmap.TADownloadBitmapHandler;
05  
06 import android.content.Context;
07 import android.view.LayoutInflater;
08 import android.view.View;
09 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ImageView;
12  
13 public class Adapter extends BaseAdapter
14 {
15   TABitmapCacheWork imageFetcher;
16     Context mContext;
17  
18     public Adapter(Context context, TAApplication application)
19     {
20         TADownloadBitmapHandler downloadBitmapFetcher = new TADownloadBitmapHandler(
21                 context, 100);
22         imageFetcher = new TABitmapCacheWork(context);
23         imageFetcher.setProcessDataHandler(downloadBitmapFetcher);
24         imageFetcher.setFileCache(application.getFileCache());
25         this.mContext = context;
26     }
27  
28     @Override
29     public int getCount()
30     {
31         // TODO Auto-generated method stub
32         return Images.imageThumbUrls.length;
33     }
34  
35     @Override
36     public Object getItem(int position)
37     {
38         // TODO Auto-generated method stub
39         return Images.imageThumbUrls[position];
40     }
41  
42     @Override
43     public long getItemId(int position)
44     {
45         // TODO Auto-generated method stub
46         return position;
47     }
48  
49     @Override
50     public View getView(int position, View convertView, ViewGroup parent)
51     {
52         // TODO Auto-generated method stub
53         LayoutInflater inflater = (LayoutInflater) mContext
54                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
55         View baseView = inflater.inflate(R.layout.login_adapter, null);
56         final ImageView imageView = (ImageView) baseView
57                 .findViewById(R.id.imageView);
58      //加载图片
59         imageFetcher.loadFormCache(getItem(position), imageView);
60         return baseView;
61     }
62 }
63  
64  
65  
66 使用
67   @TAInjectView(id = R.id.gridView)
68     GridView  gridView;
69     Adapter adapter = new Adapter(this, getTAApplication());
70     gridView.setAdapter(adapter);
 

缓存模块 方法

  • 很简单的实现缓存,以一个简单的文件缓存为例
  • 下载处理类
package com.test.file; import com.ta.util.cache.TAProcessDataHandler; //下载处理类 public class TAProcessStringHandler extends TAProcessDataHandler { @Override public byte[] processData(Object data) {  // TODO Auto-generated method stub  String mynameString="white_cat"; //这里对数据进行处理,如下载东西等等,转换为byte[]以供缓存存储使用 return mynameString.getBytes(); }  }
  • 缓存结果返回操作类
 

package com.test.file;

import android.widget.TextView;

import com.ta.util.cache.TACallBackHandler;

public class TAStringCallBackHandler extends TACallBackHandler<TextView> {   @Override     public void onStart(TextView t, Object data)     {         // TODO Auto-generated method stub         super.onStart(t, data);     }

    @Override     public void onSuccess(TextView t, Object data, byte[] buffer)     {         // TODO Auto-generated method stub         super.onSuccess(t, data, buffer);     }

    @Override     public void onFailure(TextView t, Object data)     {         // TODO Auto-generated method stub         super.onFailure(t, data);     } }

  • 程序调用
TAFileCacheWork<TextView> taFileCacheWork=new TAFileCacheWork<TextView>(); taFileCacheWork.setFileCache(getTAApplication().getFileCache()); taFileCacheWork.setCallBackHandler(new TAStringCallBackHandler()); taFileCacheWork.setProcessDataHandler(new TAProcessStringHandler()); taFileCacheWork.loadFormCache("http://www.baidu.com", textView);

打印模块使用方法

可以较快的轻易的是实现日志打印,支持日志打印的扩展,目前支持对sdcard写入本地打印、以及控制台打印 添加打印器 TALogger.addLogger(new TAPrintToFileLogger()); 调用打印 TALogger.addLogger(new TAPrintToFileLogger()); TALogger.d(TestActivity.this, "test");

下载器模块

可以简单的实现多线程下载、后台下载、断点续传、对下载进行控制、如开始、暂停、删除等等。

private DownloadManager downloadManager; downloadManager = DownloadManager.getDownloadManager();         downloadManager.setDownLoadCallback(new DownLoadCallback()         {             @Override             public void onSuccess(String url)             {

            }

            @Override             public void onl oading(String url, String speed, String progress)             {                 // TODO Auto-generated method stub

            }         });         //添加         downloadManager.addHandler(url);         //继续         downloadManager.continueHandler(url);         //暂停         downloadManager.pauseHandler(url);         //删除         downloadManager.deleteHandler(url);         //

下载器后台运行

view source
 
print?
01 private IDownloadService downloadService;
02     ServiceConnection serviceConnection=new ServiceConnection()
03     {
04          
05         @Override
06         public void onServiceDisconnected(ComponentName name)
07         {
08             // TODO Auto-generated method stub
09              
10         }
11          
12         @Override
13         public void onServiceConnected(ComponentName name, IBinder service)
14         {
15             // TODO Auto-generated method stub
16             downloadService=(IDownloadService)service;
17         }
18     };
19      
20     //添加任务
21     downloadService.addTask(url)
22     //暂停任务
23     downloadService.pauseTask(url)
24     //继续任务
25     downloadService.continueTask(url)
26     //删除任务
27     downloadService.deleteTask(url)

 

配置器模块:

可以对简易的实现配对配置的操作,目前配置文件可以支持Preference、Properties对配置进行存取

  TAIConfig config = application
                .getConfig(TAApplication.PROPERTIESCONFIG);
        Entity entity = new Entity();
        entity.setB(false);
        entity.setD(10);
        entity.setI(1);
        entity.setF(1f);
        config.setConfig(entity);
        Entity cEntity = config.getConfig(Entity.class);
        textView.setText(cEntity.toString());

网络状态检测模块: 

当网络状态改变时,对其进行检测。

view source
 
print?
01 TANetworkStateReceiver.registerObserver(new TANetChangeObserver()
02         {
03             @Override
04             public void onConnect(netType type)
05             {
06                 // TODO Auto-generated method stub
07                 super.onConnect(type);
08                 Toast.makeText(TestActivity.this, "onConnect",
09                         Toast.LENGTH_SHORT).show();
10             }
11  
12             @Override
13             public void onDisConnect()
14             {
15                 // TODO Auto-generated method stub
16                 super.onDisConnect();
17                 Toast.makeText(TestActivity.this, "onDisConnect",
18                         Toast.LENGTH_SHORT).show();
19             }
20         });
21  
22  
23         需要开启权限
24           <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
25     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
26     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
27     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
28  
29     配置:
30      <receiver android:name="com.ta.util.netstate.TANetworkStateReceiver" >
31             <intent-filter>
32                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
33             </intent-filter>
34         </receiver>

关于作者(白猫)

  • ThinkAndroid交流网站:http://www.thinkandroid.cn
  • ThinkAndroid交流QQ群 : 169415162