Android 性能优化篇
一、使用Gallery控件,如果载入的图片过多、过大、出现OOM异常。
因为Android默认分配的内存只有几兆。如果是jpg之类的,在内存中展开时就会占用大量的空间,容易内存溢出
ImageView i = new ImageView(mContext);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 10;
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString(),options);
i.setImageBitmap(bm);
bm.recycle(); //资源回收
备注:貌似这个options的功能是返回缩略图,10即表示长和宽为原来的1/10,即面积为原来的1/100。缩略图可以减少内存占用
二、统一管理位图资源,适时释放资源
class ImageManager {
private WeakHashMap> mBitmaps;
private WeakHashMap mDrawables;
private boolean mActive = true;
public ImageManager() {
mBitmaps = new WeakHashMap>();
mDrawables = new WeakHashMap>();
}
public Bitmap getBitmap(int resource) {
if (mActive) {
if (!mBitmaps.containsKey(resource)) {
mBitmaps.put(resource, new WeakReference(BitmapFactory.decodeResource(MainActivity.getContext().getResources(), resource)));
}
return ((WeakReference)mBitmaps.get(resource)).get();
}
return null;
}
public Drawable getDrawable(int resource) {
if (mActive) {
if (!mDrawables.containsKey(resource)) {
mDrawables.put(resource, new WeakReference(getApplication().getResources().getDrawable(resource)));
}
return ((WeakReference)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) e.getValue()).get().recycle();
}
mBitmaps.clear();
}
public ImageManager setActive(boolean b) {
mActive = b;
return this;
}
public boolean isActive() {
return mActive;
}
}
三、网络连接
前提:先判断网络是否可用,如果不行就不需要执行下面的操作
检查联网方式如下:
private boolean isConnected(){
ConnectivityManager mConnectivity = (ConnectivityManager) 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;
}
}
四、网络间的数据传输也是非常耗费资源的,这包括传输方式和解析方式
多用SAX解析多余DOM解析,多用json多余xml
SAX:边读取边解析
DOM:整篇读取王完毕后在根据节点层次进行解析
JSON:轻量级数据格式
五、传输数据时进行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
用AlarmManager来定时启动服务。避免service不停的去服务器上获取数据(耗流量)、不更新时让他sleep(耗电)
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);