单例模式之传递Context参数

 

单例模式之传递Context参数

  346人阅读  评论(3)  收藏  举报
  分类:

单利模式的懒汉模式,在Android的源码中应用的还是相当普遍的。在android的开发中,有时候需要一个Context的变量,同时还需要使用单例模式,那么该怎么处理呢?

  • 在getInstance的时候,进行传递context变量。这样的方式,在android源码中还是很普遍的。

/**
 * 单例模式的懒汉模式
 * 传递了一个上下文参数 context ,如此就能够实现新建一个线程,能有ui的操作
 * 参考:http://blog.csdn.net/l2show/article/details/46672061 和 android 源码
 */
public class DownloadInfo {
    private static final  String TAG  = "DownloadInfo";
    private Context mContext;
    private static DownloadInfo mDownloadInfo;
    private Handler mToastHandler;
    private static final  int DOWNLOADNOTHING = 0;
    private DownloadInfo(Context context) {
        this.mContext = context;
        //执行一些初始化的任务
        new Thread(){
          public void run(){
              Looper.prepare();
              mToastHandler = new Handler(Looper.myLooper()){
                  @Override
                  public void handleMessage(Message msg) {
                    switch (msg.what){
                        case DOWNLOADNOTHING:
                            Toast.makeText(mContext,"DOWNLOADNOTHING",Toast.LENGTH_SHORT).show();
                    }
                  }
              };
              Looper.loop();
          }
        }.start();
    }

    public static DownloadInfo getInstance(Context context){
        if(mDownloadInfo ==null){
            mDownloadInfo = new DownloadInfo(context);
        }
        return mDownloadInfo;
    }
    //建立一些方法
    public void showNothingToast(){
        if(mToastHandler != null){
            mToastHandler.sendEmptyMessage(DOWNLOADNOTHING);
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

public class MainActivity extends AppCompatActivity {

    private DownloadInfo downloadInfo;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        downloadInfo = DownloadInfo.getInstance(context);
        downloadInfo.showNothingToast();
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

以上代码能实现在一个单例的线程中更新ui界面的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值