public class Foo{
private static Foo foo;
private Context mContext;
private Foo(Context mContext){
this.mContext = mContext;
}
// 普通单例,非线程安全
public static Foo getInstance(Context mContext){
if(foo == null)
foo = new Foo(mContext);
return foo;
}
public void otherAction(){
mContext.xxxx();
….
}
}
public class Foo{
private static Foo foo;
private Context mContext;
private Foo(Context mContext){
this.mContext = mContext;
}
// 普通单例,非线程安全
public static Foo getInstance(Context mContext){
if(foo == null)
foo = new Foo(mContext.getApplicationContext());
return foo;
}
public void otherAction(){
mContext.xxxx();
….
}
}
public class FooActivity extends Activity{
private TextView textView;
private Handler handler = new Handler(){
@override
public void handlerMessage(Message msg){
}
};
@override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContextView(R.layout.activity_foo_layout);
textView = (TextView)findViewById(R.id.textView);
handler.postDelayed(new Runnable(){
@override
public void run(){
textView.setText(“ok”);
};
},1000 * 60 * 10);
}
}
public class FooActivity extends Activity{
private TextView textView;
private static class MyHandler extends Handler {
private final WeakReference<FooActivity> mActivity;
public MyHandler(FooActivity activity) {
mActivity = new WeakReference<FooActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
FooActivity activity = mActivity.get();
if (activity != null) {
// ...
}
}
}
private final MyHandler handler = new MyHandler(this);
@override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContextView(R.layout.activity_foo_layout);
textView = (TextView)findViewById(R.id.textView);
handler.postDelayed(new MyRunnable(textView),1000 * 60 * 10);
}
private static class MyRunnable implements Runnable{
private WeakReference<TextView> textViewWeakReference;
public MyRunnable(TextView textView){
textViewWeakReference = new WeakReference<TextView>(textView);
}
@override
public void run(){
final TextView textView = textViewWeakReference.get();
if(textView != null){
textView.setText("OK");
}
};
}
}
handler.removeCallbacksAndMessages(null); // removeCallbacksAndMessages,当参数为null的时候,可以清除掉所有跟次handler相关的Runnable和Message,我们在onDestroy中调用次方法也就不会发生内存泄漏了。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有