一、在使用Gallery控件时,如果载入的图片过多,过大,就很容易出现OutOfMemoryError异常,就是内存溢出。这是因为Android默认分配的内存只有几M,而载入的图片如果是JPG之类的压缩格式,在内存中展开时就会占用大量的空间,也就容易内存溢出。这时可以用下面的方法解决:
1. ImageView i = new ImageView(mContext);
2. new BitmapFactory.Options();
3. options.inSampleSize = 10;
4. //貌似这个options的功能是返回缩略图,10即表示长和宽为原来的1/10,即面积为原来的1/100
5. //缩略图可以减少内存占用
6. Bitmap bm = BitmapFactory.decodeFile(lis.
7. get(position).toString(),options);
8. i.setImageBitmap(bm);
9. bm.recycle();
10. //资源回收
二、统一管理位图资源,适时释放资源
1. class ImageManager {
2. private WeakHashMap<Integer, WeakReference<Bitmap>> mBitmaps;
3. private WeakHashMap<Integer, WeakReference<Drawable》> mDrawables;
4.
5. private boolean mActive = true;
6.
7. public ImageManager() {
8. new WeakHashMap<Integer, WeakReference<Bitmap>>();
9. new WeakHashMap<Integer, WeakReference<Drawable>>();
10. }
11.
12.
13. public Bitmap getBitmap(int resource) {
14. if (mActive) {
15. if (!mBitmaps.containsKey(resource)) {
16. mBitmaps.put(resource,
17. new WeakReference<Bitmap>(BitmapFactory.decodeResource(MainActivity.getContext().getResources(), resource)));
18. }
19. return ((WeakReference<Bitmap>)mBitmaps.get(resource)).get();
20. }
21. return null;
22. }
23.
24. public Drawable getDrawable(int resource) {
25. if (mActive) {
26. if (!mDrawables.containsKey(resource)) {
27. new WeakReference<Drawable>(getApplication().getResources().getDrawable(resource)));
28. }
29. return ((WeakReference<Drawable>)mDrawables.get(resource)).get();
30. }
31. return null;
32. }
33.
34. public void recycleBitmaps() {
35. Iterator itr = mBitmaps.entrySet().iterator();
36. while (itr.hasNext()) {
37. Map.Entry e = (Map.Entry)itr.next();
38. get().recycle();
39. }
40. mBitmaps.clear();
41. }
42.
43. public ImageManager setActive(boolean b) {
44. mActive = b;
45. return this;
46. }
47.
48. public boolean isActive() {
49. return mActive;
50. }
51. }
三、网络连接往往是耗电量比较大的 那我们可以优化一下在需要网络连接的程序中,首先检查网络连接是否正常,如果没有网络连接,那么就不需要执行相应的程序。
检查网络连接的方法如下:
1. private boolean isConnected(){
2. this.getSystemService(CONNECTIVITY_SERVICE);
3. TelephonyManager mTelephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
4.
5. // 检查网络连接,如果无网络可用,就不需要进行连网操作等
6. NetworkInfo info = mConnectivity.getActiveNetworkInfo();
7. if (info == null ||
8. !mConnectivity.getBackgroundDataSetting()) {
9. return false;
10. }
11. //判断网络连接类型,只有在3G或wifi里进行一些数据更新。
12. int netType = info.getType();
13. int netSubtype = info.getSubtype();
14. if (netType == ConnectivityManager.TYPE_WIFI) {
15. return info.isConnected();
16. else if (netType == ConnectivityManager.TYPE_MOBILE
17. && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
18. && !mTelephony.isNetworkRoaming()) {
19. return info.isConnected();
20. else {
21. return false;
22. }
23. }
四、网络间的数据传输也是非常耗费资源的,这包括传输方式和解析方式
来看一个表格
其中 Tree Parse 是DOM解析 Event/Stream是SAX方式解析
很明显,使用流的方式解析效率要高一些,因为DOM解析是在对整个文档读取完后,再根据节点层次等再组织起来。而流的方式是边读取数据边解析,数据读取完后,解析也就完毕了。
在数据格式方面,JSON和Protobuf效率明显比XML好很多,XML和JSON大家都很熟悉。
从上面的图中我们可以得出结论就是尽量使用SAX等边读取边解析的方式来解析数据,针对移动设备,最好能使用JSON之类的轻量级数据格式为佳。
五、传输数据经过压缩 目前大部门网站都支持GZIP压缩,所以在进行大数据量下载时,尽量使用GZIP方式下载,可以减少网络流量。
使用方法如下所示:
1. HttpGet request =
2. new HttpGet("http://example.com/gzipcontent");
3. HttpResponse resp =
4. new DefaultHttpClient().execute(request);
5. HttpEntity entity = response.getEntity();
6. InputStream compressed = entity.getContent();
7. new GZIPInputStream(compressed);
六、有效管理Service 后台服务就相当于一个持续运行的Acitivity 如果开发的程序后台都会一个service不停的去服务器上更新数据,在不更新数据的时候就让它sleep,这种方式是非常耗电的,通常情况下,我们可以使用AlarmManager来定时启动服务。如下所示,第30分钟执行一次。
1. AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
2. new Intent(context, MyService.class);
3. PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
4. long interval = DateUtils.MINUTE_IN_MILLIS * 30;
5. long firstWake = System.currentTimeMillis() + interval;
6. am.setRepeating(AlarmManager.RTC,firstWake, interval, pendingIntent);
开发过程中应该注意一些细节,并经手机的整体性能和续航都是有很大的局限,很多个优化的细节会对软件产生本质的影响,这些需要引起重视,也要在开发过程中不断积累