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. django基础-02:虚拟环境

    4. 虚拟环境 4.1 概念 之前安装python包的命令: sudo pip3 install 包名 包的安装路径:/usr/local/lib/python3.5/dist-packages 在同 ...

  2. find 命令 查找

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

  3. 【Oracle】使用bbed手动提交事务

    有时候数据库挂掉,起库会出现ORA-00704错误,而导致ORA-00704错误的根本原因是訪问OBJ$的时候.ORACLE须要回滚段中的数据,而訪问回滚段的时候须要的undo数据已经被覆盖,此时我们 ...

  4. 20171018 在小程序页面去获取用户的OpenID

    1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...

  5. shell脚本编写实例

    实际案例 1.判断接收参数个数大于1 [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.. ...

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

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

  7. Hive SQL测试

    在spark的空表test上进行运算,注意结果差异: ,age)) as ages from test group by name;//空 ,age)) as ages from test group ...

  8. webstorm2018版安装-破解

    安装完成后到下面网址下载破解补丁 网址:http://idea.lanyus.com/ 修改路径 修改同目录下的 WebStorm.exe.vmoptions 和WebStorm64.exe.vmop ...

  9. DL中train\dev\test集

    转自:https://blog.csdn.net/l8947943/article/details/80328721 training set:训练集是用来训练模型的.遵循训练集大,开发,测试集小的特 ...

  10. kfrobotaidlog查找

    ======查看最新的大于6的数量========== select c.questionid from (select a.questionid, b.clientQuestion from sim ...