RXAndroid之RxSharedPreference的使用

        //创建sharedPreference实例
        SharedPreferences mySP = getApplicationContext().getSharedPreferences("rx_sp_test", Context.MODE_PRIVATE);
        mySP.edit().putString("test_string","this is test rx string").commit();

        mySP.edit().putBoolean("test_boolean",true).commit();
        //将sharedPreference作为参数转换成RxSharedPreferences实例
        RxSharedPreferences rxPreferences = RxSharedPreferences.create(mySP);

        //获取保存的值
        Preference<String> testString = rxPreferences.getString("test_string");
        Preference<Boolean> testBoolean = rxPreferences.getBoolean("test_boolean", false);

        //发射数据
        testString.asObservable()
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String username) {
                        Log.e(TAG, "test_string: " + username);
                    }
                });

        testBoolean.asObservable()
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        Log.e(TAG, "test_boolean: " + aBoolean);
                    }
                });


运行结果:



注:1.需要在build.gradle中添加compile 'com.f2prateek.rx.preferences:rx-preferences:1.0.2'

        2.参考资料:https://github.com/ReactiveX/RxAndroid/wiki#removed

https://github.com/f2prateek/rx-preferences

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值