Android获取全局Context的方法
Android获取全局Context的方法
Android--应用全局获取Context - 超宇的博客 - CSDN博客
https://blog.csdn.net/chaoyu168/article/details/64444274
如何构建Android MVVM 应用框架 -
https://tech.meituan.com/android_mvvm.html
看这篇文章说的还是需要每个ViewModel需要持了一个Context的引用
但应该可以做个ViewModel内都可以使用的,不用每个方法都传入那么麻烦
使用mvvm框架可以参考下面的实现方式,得改几个地方比较繁琐
=====================
实例:
AppComponent增加 FavoritesDBManager FavoritesDBManager();
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
SharedPreferences sharedPreferences();
Context context();
FavoritesDBManager FavoritesDBManager();
}
---------------------
CoinListComponent增加注解:dependencies = AppComponent.class
@FragmentScope
@Component(modules = CoinListModule.class, dependencies = AppComponent.class)
public interface CoinListComponent {
----------------
全局AppModule增加provideFavoritesDBManager
@Module
public class AppModule {
private Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
@Singleton
FavoritesDBManager provideFavoritesDBManager() {
return new FavoritesDBManager(context);
}
----------------
CoinListViewModel 增加@Inject FavoritesDBManager mFavoritesDBManager;
用到的的地方直接调用
public class CoinListViewModel extends BaseViewModel implements CoinlistItemClickLisnter {
@Inject
FavoritesDBManager mFavoritesDBManager;
if(status == 0) {
List<Favorites> listData = mFavoritesDBManager.getFavoritesListData();
}
=====================
java.lang.IllegalStateException: com.aax.exchange.inject.AppComponent must be set
at com.aax.exchange.inject.component.DaggerCoinListComponent$Builder.build(DaggerCoinListComponent.java:75)
at com.aax.exchange.fragment.CoinListFragment.inject(CoinListFragment.java:56)
at com.aax.exchange.base.BaseMvvmFragment.onCreateView(BaseMvvmFragment.java:54)
---------------------
用到的地方CoinSearchActivity,CoinListFragment都要加上.appComponent(ComponentHolder.getComponent())
--------------
@Override
protected void inject() {
CoinSearchComponent component = DaggerCoinSearchComponent.builder()
.coinSearchModule(new CoinSearchModule(this)).appComponent(ComponentHolder.getComponent())
.build();
@Override
protected void inject() {
CoinListComponent component = DaggerCoinListComponent.
builder().coinListModule(new CoinListModule(this)).appComponent(ComponentHolder.getComponent())
.build();
------------------
public class ComponentHolder {
private static AppComponent mComponent;
public static AppComponent getComponent() {
return mComponent;
}
public static void setComponent(AppComponent component) {
mComponent = component;
}
}
=====================
subscribe(fragment.mAct,Api.getApiService().getFavorites(new ObserverResponseListener<Object>() {
@Override
public void onNext(Object o) {
ToastUtil.showLongToast("get favorites ");
String tradingPair = data.getQuote() + data.getBase();
Favorites fav = new Favorites();
fav.setTradingPair(tradingPair);
mFavoritesDBManager.addFavoritesData(fav);
}
@Override
public void onError(Throwable e) {
ToastUtil.showLongToast("get favorites error");
}
}, fragment.bindToLifecycle(),false,false);
Android获取全局Context的方法的更多相关文章
- Android 获取全局Context的技巧
回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要.使用通知的时候需要等等等等.或许目前你 ...
- Android设置全局Context
新建一个java继承Application类 import android.app.Application; import android.content.Context; /** * 编写自定义Ap ...
- Android获取网络时间的方法
一.通过免费或者收费的API接口获取 1.免费 QQ:http://cgi.im.qq.com/cgi-bin/cgi_svrtime 淘宝:http://api.m.taobao.com/rest/ ...
- Android获取位置信息的方法总结
1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...
- android获取sd卡路径方法
public String getSDPath(){ File sdDir = null; boolean sdCardExist = Environment.getExternalStorage ...
- Android 获取唯一标识替代方法
private static String getTheOnlyID() { String onlyOne; //获取IMEI TelephonyManager TelephonyMgr = (Tel ...
- 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息
项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...
- Android 获取SD卡路径和推断SD卡是否存在
android获取sd卡路径方法: 不建议直接写死android sd卡的路径. public String getSDPath(){ File sdDir = null; boolean sdCar ...
- Android获取Context(任意位置任意地方,全局上下文)
一般获取context的方法 1.Activity.this的context (一般用法)返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁 2.getAppli ...
随机推荐
- objenesis
1.objenesis About Objenesis is a small Java library that serves one purpose: To instantiate a new ob ...
- CentOS 系列安装 Docker
CentOS6对于 CentOS6,可以使用 EPEL 库安装 Docker,命令如下$ sudo yum install http://mirrors.yun-idc.com/epel/6/i386 ...
- (4.2)mysql备份还原——备份概述
1.什么情况下会用到备份呢? [1.1]灾难恢复 [1.2]单位审计:数据库在过去某一个点是什么样的 [1.3]跨机房灾备:异地备份 [1.4]认为的DDL或者DML语句,导致主从库的数据消失 [1. ...
- 数据库 - Navicat与pymysql模块
一.Nabicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时, 可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 官网下载:htt ...
- 004-mac上安装以及Nginx 配置文件nginx.conf详解
1.mac上nginx安装 安装brew:go-001-环境部署,IDEA插件 安装nginx: brew search nginx brew install nginx 当然也可以编译安装 安装完以 ...
- VGA线 1080P之伤 <中秋节篇>
故障:通过一台笔记本,在一台高清的电视机上使用VGA线进行视频传输,分辩率最高只能显示1600*1200,为什么不是1080P? 哎,我也很郁闷,查了相关的资料~电视机是最高支持1080P分辩率的,笔 ...
- vmware 下linux 共享文件夹消失
今天遇到了vmware下linux和宿主win7系统共享文件,突然在linux下消失的问题 环境:vmware10.0.0下装了centos, 主机系统是win7. 背景:事情的初衷是想让win7 下 ...
- python SMTP attachment
发邮件,现在还有不带附件的吗? 开个玩笑,你要带,就得如此下边这样办 //test.py import smtplib from email.mime.text import MIMEText fro ...
- 判断数组对象里面的某个属性全部为true才执行下一步操作
比如数据[ {name:'张三',isshow:'false'},name:'李四',isshow:'false'}, ] 这里是自己写的验证,没用elemten的 如果有2张票,需要刷2张身份证,则 ...
- react native 中使用swiper
1.下载依赖 cnpm install react-native-swiper --save 2.在组件中使用 import React, { Component } from 'react'; im ...