gpt4 book ai didi

android - 获取 SyncAdapter 中的上下文

转载 作者:行者123 更新时间:2023-11-29 19:58:10 25 4
gpt4 key购买 nike

我正在构建一个自定义 MySyncAdapter,它主要基于 the guide 中的那个。 .在同步操作中,我想从 SharedPreferences 获得偏好,但是 PreferenceManager.getDefaultSharedPreferences() 需要一个 context,而我无法获得使用任何常用方法:

  • 这个
  • MainActivity.this
  • getApplicationContext()

如何在 SyncAdapter 中获取 context

最佳答案

SyncAdapter 实际上确实从外部获取上下文,只是不在同步方法中:

public MySyncAdapter(Context context, boolean autoInitialize)

在构造函数中,我只需要将context赋值给this.mContext即可,可以在sync方法中引用。

关于android - 获取 SyncAdapter 中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36533596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com