Android进阶:性能优化篇
2011-08-09 17:06
453 查看
一、在使用Gallery控件时,如果载入的图片过多,过大,就很容易出现OutOfMemoryError异常,就是内存溢出。这是因为Android默认分配的内存只有几M,而载入的图片如果是JPG之类的压缩格式,在内存中展开时就会占用大量的空间,也就容易内存溢出。这时可以用下面的方法解决:
class ImageManager { private WeakHashMap<Integer, WeakReference<Bitmap>> mBitmaps; private WeakHashMap<Integer, WeakReference<Drawable》> mDrawables; 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 (!mDrawables.containsKey(resource)) { mDrawables.put(resource, new WeakReference<Drawable>(getApplication().getResources().getDrawable(resource))); } return ((WeakReference<Drawable>)mDrawables.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; } }
三、网络连接往往是耗电量比较大的 那我们可以优化一下在需要网络连接的程序中,首先检查网络连接是否正常,如果没有网络连接,那么就不需要执行相应的程序。
检查网络连接的方法如下:
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 后台服务就相当于一个持续运行的Acitivity 如果开发的程序后台都会一个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;
long firstWake = System.currentTimeMillis() + interval;
am.setRepeating(AlarmManager.RTC,firstWake, interval, pendingIntent);
开发过程中应该注意一些细节,并经手机的整体性能和续航都是有很大的局限,很多个优化的细节会对软件产生本质的影响,这些需要引起重视,也要在开发过程中不断积累
相关文章推荐
- Android进阶——性能优化之尽量多使用AsyncTask进行短时间网络通信
- Android进阶——性能优化之多线程总结及简单应用(一)
- Android进阶:性能优化篇
- Android进阶:性能优化篇
- Android高手进阶——Android内存性能优化
- Android高手进阶——Android内存性能优化
- [Android进阶]Android性能优化
- Android技术专题]应用开发进阶必经之路之性能优化
- android进阶----性能优化----电量优化
- Android 进阶第二篇——性能优化
- Android高手进阶——Android内存性能优化
- Android进阶#(6/12)让程序更优的技术——性能优化_布局优化
- android进阶---性能优化-----ui
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- Android进阶#(6/12)让程序更优的技术——性能优化_内存优化
- Android进阶#(6/12)让程序更优的技术——性能优化_内存泄漏
- [Android优化进阶] 提高ListView性能的技巧
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- Android进阶-Android性能优化
- Android进阶之性能优化