Android优化系列——性能优化(图片缓存,网络链接,Service定时)
2015-06-15 23:35
549 查看
( 整理来自Android优化技术详解一书)
在使用Gallery控件时,如果加载的图片过多,过大,就容易出现OutOfMemoryError异常,就是内存溢出。
实现统一管理位图资源,适时释放资源
弱引用也是用来描述非必需对象的,但是它的强度比软引用更弱一些,被弱引用关联的对象只能生存到下一次垃圾收集发生之前。当垃圾收集器工作时,无论当前内存是否足够,都会回收掉只被弱引用关联的对象。在JDK 1.2之后,提供了WeakReference类来实现弱引用。
检查网络链接的方法
目前大部分网站都支持GZIP压缩,所以在进行大数据量下载时,尽量使用GZIP方式下载,可以减少网络流量。
有效管理service后台服务就相当于持续运行的Activity。如果开发程序后台都有一个service不停地去服务器上更新数据,在不更新数据的时候就让它sleep,这种方式是非常耗电的。我们可以使用AlarmManager来定时启动服务,每30分钟执行一次。
在使用Gallery控件时,如果加载的图片过多,过大,就容易出现OutOfMemoryError异常,就是内存溢出。
ImageView i = new ImageView(mContext); BitmapFactory.Options options = new BitmapFactory.Options(); //返回缩率图,长宽为原来的10分之一,面积为1/100 options.inSampleSize = 10; Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString(),options); i.setImageBitmap(bm); bm.recycle();
实现统一管理位图资源,适时释放资源
class ImageManager{ //弱引用 private WeakHashMap<Integer, WeakReference<Bitmap>> mBitmaps; private WeakHashMap<Integer, WeakReference<Drawable>> mDrawable; private boolean mActive = true; public ImageManager(){ mBitmaps = new WeakHashMap<Integer, WeakReference<Bitmap>>(); mDrawables = new WeakHashMap<Integer, WeakReference<Drawable>>(); } public Bitmap getBitmap(int resource){ if(mActive){ if(!mBitmaps.containsKey(resource)){ mBitmaps.put(resource, new WeakReference<Bitmap>(BitmapFactory.decodeResource(MainActivity.getContext().getResources(),resource))); } return ((WeakReference<Bitmap>)mBitmaps.get(resource)).get(); } return null; } public Drawable getDrawable(int resource){ if(mActive){ if(!mBitmaps.containsKey(resource)){ mBitmaps.put(resource, new WeakReference<Drawable>(getApplication().getResources().getDrawable(resource))); } return ((WeakReference<Drawable>)mBitmaps.get(resource)).get(); } return null; } public void recycleBitmaps(){ Iterator itr = mBitmaps.entrySet().iterator(); while(itr.hasNext()){ Map.Entry e = (Map.Entry)itr.next(); ((WeakReference<Bitmap>) e.getValue()).get().recycle(); } mBitmaps.clear(); } public ImageManager setActive(boolean b){ mActive = b; return this; } public boolean isActive(){ return mActive; } }
弱引用也是用来描述非必需对象的,但是它的强度比软引用更弱一些,被弱引用关联的对象只能生存到下一次垃圾收集发生之前。当垃圾收集器工作时,无论当前内存是否足够,都会回收掉只被弱引用关联的对象。在JDK 1.2之后,提供了WeakReference类来实现弱引用。
检查网络链接的方法
private boolean isConnected(){ ConnectivityManger mConnectivity = (ConnectivityManger)this.getSystemService(CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); //检查网络链接,如果无网络可用,就不需要链接网络 NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if(info == null||!mConnectivity.getBackgroundDataSetting()){ return false; } //判断网络连接类型,只有在3G与wifi里进行一些数据更新 int netType = info.getType(); int netSubtype = info.getSubtype(); if(netType == ConnectivityManager.TYPE_WIFI){ return info.isConnected(); }else if(netType == ConnectivityManager.TYPE_MOBILE&&netSubtype == TelephonyManager.NETWORK_TYPE_UMTS&&!mTelephony.isNetworkRoaming()){ return info.isConnected(); }else{ return false; } }
目前大部分网站都支持GZIP压缩,所以在进行大数据量下载时,尽量使用GZIP方式下载,可以减少网络流量。
HttpGet request = new HttpGet("http://example.com/gzipcontent"); HttpResponse resp = new DefaultHttpClient().execute(request); HttpEntity entity = response.getEntity(); InputStream compressed = entity.getContent(); InputStream rawData = new GZIPInputStream(compressed);
有效管理service后台服务就相当于持续运行的Activity。如果开发程序后台都有一个service不停地去服务器上更新数据,在不更新数据的时候就让它sleep,这种方式是非常耗电的。我们可以使用AlarmManager来定时启动服务,每30分钟执行一次。
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, MyService.class); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); long interval = DateUtils.MINUTE_IN_MILLIS*30 4000 ; long firstWake = System.currentTimeMillis()+interval; am.setRepeating(AlarmManager.RTC, firstWake, interval, pendingIntent);
相关文章推荐
- MySQL 优化
- Google排名优化的几个影响因素
- DB2优化(简易版)
- Mysql limit 优化,百万至千万级快速分页 复合索引的引用并应用于轻量级框架
- C#中尾递归的使用、优化及编译器优化
- 优化Ruby脚本效率实例分享
- mysql -参数thread_cache_size优化方法 小结
- 详解mysql的limit经典用法及优化实例
- oracle数据库sql的优化总结
- SQL优化技巧指南
- SQL Server优化50法汇总
- C++快速排序的分析与优化详解
- 手把手教你如何优化C语言程序
- mysql 分页优化解析
- 非常不错的MySQL优化的8条经验
- 优化Node.js Web应用运行速度的10个技巧
- JavaScript学习笔记(十七)js 优化
- 如何改进javascript代码的性能
- javascript代码加载优化方法
- 关于Javascript加载执行优化的研究报告