码迷,mamicode.com
首页 > 移动开发 > 详细

Android Handler 防内存泄漏

时间:2016-04-09 09:19:35      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

package com.tv.ui.metro.utils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.thunder.ktv.thunderjni.thunderapi.TDHotspot;
import com.tv.ui.metro.MainActivity;
import com.tv.ui.metro.model.GenericSubjectItem;
import com.tv.ui.metro.model.KtvMainModuleDisplayItem;
import com.tv.ui.metro.view.KtvLogoView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
* Created by Administrator on 16-4-5.
*/
public class ActivityHelper {
public static class MainHandler extends Handler {

private static MainHandler _this;
public static final int MAIN_MODULE_JSON_READY = 1;
public static final int LEFT_TOP_HST_READY = 2;
public static String KEY_RAW_JSON = "raw_json";
public static String KEY_LEFT_TOP_LIST = "lt_list";
WeakReference<Activity> mActivityReference;

MainHandler(Activity activity) {
super(activity.getMainLooper());
mActivityReference = new WeakReference<Activity>(activity);
}

public static MainHandler getInstance(Activity activity) {
if (_this == null) {
_this = new MainHandler(activity);
}
return _this;
}

@Override
public void handleMessage(Message msg) {
final MainActivity activity = (MainActivity) mActivityReference.get();
Bundle b;
if (activity != null) {
switch (msg.what) {
case MAIN_MODULE_JSON_READY:
Log.d("MainActivity", "handleMessage MAIN_MODULE_JSON_READY");
//更新UI
b = msg.getData();
GenericSubjectItem<KtvMainModuleDisplayItem> json = (GenericSubjectItem<KtvMainModuleDisplayItem>) b.getSerializable(KEY_RAW_JSON);
activity.updateTabsAndMetroUI(json);
activity.getTabHost().requestLayout();
final View tabView = activity.getTabs().getChildTabViewAt(activity.getViewPager().getCurrentItem());
tabView.post(new Runnable() {
@Override
public void run() {
tabView.requestFocus();
}
});
break;
case LEFT_TOP_HST_READY:
Log.d("MainActivity", "handleMessage LEFT_TOP_HST_READY");
b = msg.getData();
b.getSerializable(KEY_LEFT_TOP_LIST);
KtvLogoView lv = activity.getLogoView();
lv = new KtvLogoView(activity);
activity.setLogoView(lv);
lv.setHsList((ArrayList<TDHotspot>) b.getSerializable("lt_list"));
break;
}
}
}
}
}

Android Handler 防内存泄漏

标签:

原文地址:http://www.cnblogs.com/----------/p/5370623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!