轉自:http://blog.csdn.net/leilu2008/article/details/6673177
一、在使用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;
-
}
-
}
三、網絡連接往往是耗電量比較大的 那我們可以優化一下在需要網絡連接的程序中,首先檢查網絡連接是否正常,如果沒有網絡連接,那麼就不需要執行相應的程序。
檢查網絡連接的方法如下:
-
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;
-
}
-
-
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;
-
}
-
}
四、網絡間的數據傳輸也是非常耗費資源的,這包括傳輸方式和解析方式
來看一個表格
其中 Tree Parse 是DOM解析 Event/Stream是SAX方式解析
很明顯,使用流的方式解析效率要高一些,因爲DOM解析是在對整個文檔讀取完後,再根據節點層次等再組織起來。而流的方式是邊讀取數據邊解析,數據讀取完後,解析也就完畢了。
在數據格式方面,JSON和Protobuf效率明顯比XML好很多,XML和JSON大家都很熟悉。
從上面的圖中我們可以得出結論就是儘量使用SAX等邊讀取邊解析的方式來解析數據,針對移動設備,最好能使用JSON之類的輕量級數據格式爲佳。
五、傳輸數據經過壓縮 目前大部門網站都支持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 後臺服務就相當於一個持續運行的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);
開發過程中應該注意一些細節,並經手機的整體性能和續航都是有很大的侷限,很多個優化的細節會對軟件產生本質的影響,這些需要引起重視,也要在開發過程中不斷積累