获取 SyncAdapter 中的上下文
Get context inside an SyncAdapter
我正在根据 the guide 中的那个构建自定义 MySyncAdapter
。在同步操作中,我想从 SharedPreferences
获得偏好,但是 PreferenceManager.getDefaultSharedPreferences()
需要 context
,而我无法通过任何常用方法获得:
this
MainActivity.this
getApplicationContext()
如何在 SyncAdapter
中获取 context
?
SyncAdapter
实际上确实从外部获取上下文,只是不在同步方法中:
public MySyncAdapter(Context context, boolean autoInitialize)
在构造函数中,我只需要将context
赋值给this.mContext
即可,可以在sync方法中引用。
我正在根据 the guide 中的那个构建自定义 MySyncAdapter
。在同步操作中,我想从 SharedPreferences
获得偏好,但是 PreferenceManager.getDefaultSharedPreferences()
需要 context
,而我无法通过任何常用方法获得:
this
MainActivity.this
getApplicationContext()
如何在 SyncAdapter
中获取 context
?
SyncAdapter
实际上确实从外部获取上下文,只是不在同步方法中:
public MySyncAdapter(Context context, boolean autoInitialize)
在构造函数中,我只需要将context
赋值给this.mContext
即可,可以在sync方法中引用。