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的方法的更多相关文章

  1. Android 获取全局Context的技巧

    回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要.使用通知的时候需要等等等等.或许目前你 ...

  2. Android设置全局Context

    新建一个java继承Application类 import android.app.Application; import android.content.Context; /** * 编写自定义Ap ...

  3. Android获取网络时间的方法

    一.通过免费或者收费的API接口获取 1.免费 QQ:http://cgi.im.qq.com/cgi-bin/cgi_svrtime 淘宝:http://api.m.taobao.com/rest/ ...

  4. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...

  5. android获取sd卡路径方法

    public String getSDPath(){  File sdDir = null;  boolean sdCardExist = Environment.getExternalStorage ...

  6. Android 获取唯一标识替代方法

    private static String getTheOnlyID() { String onlyOne; //获取IMEI TelephonyManager TelephonyMgr = (Tel ...

  7. 玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息

    项目的Github地址:https://github.com/ggrcwxh/LastTime 采用基于git的多人协作开发模式 软件采用mvc设计模式,前端这么艺术的事我不太懂,交给斌豪同学去头疼了 ...

  8. Android 获取SD卡路径和推断SD卡是否存在

    android获取sd卡路径方法: 不建议直接写死android sd卡的路径. public String getSDPath(){ File sdDir = null; boolean sdCar ...

  9. Android获取Context(任意位置任意地方,全局上下文)

    一般获取context的方法 1.Activity.this的context (一般用法)返回当前activity的上下文,属于activity ,activity 摧毁他就摧毁 2.getAppli ...

随机推荐

  1. windows安装nginx并存放静态资源

    1.将nginx-windows.zip下载下来,然后点击nginx.exe 如果一闪而过并且打开网页输入localhost显示无法访问,打开error.log文件:No mapping for th ...

  2. 异步fifo的Verilog实现

     一.分析 由于是异步FIFO的设计,读写时钟不一样,在产生读空信号和写满信号时,会涉及到跨时钟域的问题,如何解决? 跨时钟域的问题:由于读指针是属于读时钟域的,写指针是属于写时钟域的,而异步FIFO ...

  3. MySQL crash-safe replication【转载】

    本文来自david大神的博客,innodb技术内幕的作者. http://insidemysql.blog.163.com/blog/static/202834042201385190333/ MyS ...

  4. find 命令 查找

    find 查找文件和目录 find /home -name "" find 后接查找的目录,-name 后指定需要查找的文件名 文件名可以用*表示所有find /home -nam ...

  5. 两个js冲突怎么解决?试试这四个方法

    两个js冲突很让前端头疼,虽然jquery是通用的,但调用不同经常会出问题.jQuery是目前流行的JS封装包,简化了很多复杂的JS程序,JQuery讲浏览器DOM树定义为$,通过$来获取各个子节点. ...

  6. Uncontrolled memory mapping in camera driver (CVE-2013-2595)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/hu3167343/article/details/34434235 /* 本文章由 莫灰灰 编写,转 ...

  7. c#4.0 Task.Factory.StartNew 用法

    var t1 = Task.Factory.StartNew<string>(() => { return “1111111”; }); //t1.Wait(); t1.Contin ...

  8. mac chrome 浏览器开启允许跨域

    在控制台输入下面代码: open -n /Applications/Google\ Chrome.app/ --args --disable-web-security  --user-data-dir ...

  9. Lua 随机数生成问题

    原文链接:http://blog.csdn.net/zhangxaochen/article/details/8095007 Lua 生成随机数需要用到两个函数: math.randomseed(xx ...

  10. 对比损失(Contrastive Loss)学习【转载】

    转自:https://blog.csdn.net/autocyz/article/details/53149760 https://www.tensorflow.org/tutorials/repre ...