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模块
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() |
11 | public void onStart(TAResponse response) |
18 | public void onSuccess(TAResponse response) |
25 | public void onRuning(TAResponse response) |
32 | public void onFailure(TAResponse response) |
38 | }, false , true , true ); |
43 | package com.ta.mvc.command; |
45 | import com.ta.mvc.common.TAIResponseListener; |
46 | import com.ta.mvc.common.TARequest; |
47 | import com.ta.mvc.common.TAResponse; |
49 | public class TAIdentityCommand extends TACommand |
52 | protected void executeCommand() |
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); |
65 | protected void notifyListener( boolean success) |
67 | TAIResponseListener responseListener = getResponseListener(); |
68 | if (responseListener != null ) |
70 | onComandUpdate(command_success); |
ioc模块使用方法:
- 完全注解方式就可以进行UI绑定、res中的资源的读取、以及对象的初始化。
01 | public class ThinkAndroidDemoActivity extends TAActivity { |
06 | @TAInjectResource (R.string.app_name) |
08 | @TAInjectResource (R.attr.test) |
10 | @TAInjectView (R.id.add); |
数据库模块
- android中的orm框架,使用了线程池对sqlite进行操作。
public class ThinkAndroidDemoActivity extends TAActivity { TASQLiteDatabasePool sqlitePool = getTAApplication() .getSQLiteDatabasePool(); TASQLiteDatabase sqliteDatabase=sqlitePool.getSQLiteDatabase(); //使用中 sqliteDatabase.insert(entity); sqlitePool.returnSQLiteDatabase(sqliteDatabase); }
Http模块使用方法:
异步get方法
01 | AsyncHttpClient client = new AsyncHttpClient(); |
02 | client.get( "http://www.thinkandroid.cn/" , new AsyncHttpResponseHandler() |
05 | public void onSuccess(String content) |
08 | super .onSuccess(content); |
09 | TALogger.d(LoginActivity. this , content); |
20 | public void onFailure(Throwable error) |
23 | super .onFailure(error); |
27 | public void onFinish() |
同步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(); }
});
文件上传到服务器,服务器如何接收,请查看这里
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() |
11 | public void onSuccess(String content) |
14 | super .onSuccess(content); |
15 | TALogger.d(LoginActivity. this , content); |
26 | public void onFailure(Throwable error) |
29 | super .onFailure(error); |
33 | public void onFinish() |
使用http下载文件:
01 | AsyncHttpClient syncHttpClient = new AsyncHttpClient(); |
02 | FileHttpResponseHandler fHandler = new FileHttpResponseHandler( |
03 | TAExternalOverFroyoUtils.getDiskCacheDir(TestActivity. this , |
04 | "sdfsdfsdf" ).getAbsolutePath()) |
09 | public void onProgress(String speed, String progress) { |
11 | super .onProgress(speed, progress); |
12 | TALogger.v(TestActivity. this , progress + "--------" + speed); } |
15 | public void onFailure(Throwable error) { |
17 | super .onFailure(error); } |
20 | public void onSuccess( byte [] binaryData) { |
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" , |
28 | fHandler.setInterrupt(interrupt); |
- imageview加载图片的时候无需考虑图片加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象。
02 | import com.ta.TAApplication; |
03 | import com.ta.util.bitmap.TABitmapCacheWork; |
04 | import com.ta.util.bitmap.TADownloadBitmapHandler; |
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; |
13 | public class Adapter extends BaseAdapter |
15 | TABitmapCacheWork imageFetcher; |
18 | public Adapter(Context context, TAApplication application) |
20 | TADownloadBitmapHandler downloadBitmapFetcher = new TADownloadBitmapHandler( |
22 | imageFetcher = new TABitmapCacheWork(context); |
23 | imageFetcher.setProcessDataHandler(downloadBitmapFetcher); |
24 | imageFetcher.setFileCache(application.getFileCache()); |
25 | this .mContext = context; |
32 | return Images.imageThumbUrls.length; |
36 | public Object getItem( int position) |
39 | return Images.imageThumbUrls[position]; |
43 | public long getItemId( int position) |
50 | public View getView( int position, View convertView, ViewGroup parent) |
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); |
59 | imageFetcher.loadFormCache(getItem(position), imageView); |
67 | @TAInjectView (id = R.id.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); //
下载器后台运行
01 | private IDownloadService downloadService; |
02 | ServiceConnection serviceConnection= new ServiceConnection() |
06 | public void onServiceDisconnected(ComponentName name) |
13 | public void onServiceConnected(ComponentName name, IBinder service) |
16 | downloadService=(IDownloadService)service; |
21 | downloadService.addTask(url) |
23 | downloadService.pauseTask(url) |
25 | downloadService.continueTask(url) |
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());
网络状态检测模块:
当网络状态改变时,对其进行检测。
01 | TANetworkStateReceiver.registerObserver( new TANetChangeObserver() |
04 | public void onConnect(netType type) |
07 | super .onConnect(type); |
08 | Toast.makeText(TestActivity. this , "onConnect" , |
09 | Toast.LENGTH_SHORT).show(); |
13 | public void onDisConnect() |
17 | Toast.makeText(TestActivity. this , "onDisConnect" , |
18 | Toast.LENGTH_SHORT).show(); |
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" /> |
30 | <receiver android:name= "com.ta.util.netstate.TANetworkStateReceiver" > |
32 | <action android:name= "android.net.conn.CONNECTIVITY_CHANGE" /> |
关于作者(白猫)
- ThinkAndroid交流网站:http://www.thinkandroid.cn
- ThinkAndroid交流QQ群 : 169415162