我的SyncAdapter更新优先级:
class SyncAdapter extends AbstractThreadedSyncAdapter { private int PARTICIPANT_ID; private final Context mContext; private final ContentResolver mContentResolver; public SyncAdapter(Context context,boolean autoInitialize) { super(context,autoInitialize); mContext = context; mContentResolver = context.getContentResolver(); } public SyncAdapter(Context context,boolean autoInitialize,boolean allowParallelSyncs) { super(context,autoInitialize,allowParallelSyncs); mContext = context; mContentResolver = context.getContentResolver(); } @OverrIDe public voID onPerformSync(Account account,Bundle extras,String authority,ContentProvIDerClIEnt provIDer,SyncResult syncResult) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); PARTICIPANT_ID = Integer.parseInt(prefs.getString("participant_ID","0")); if (success) { // save and set the new participant ID PARTICIPANT_ID = newParticipantID; prefs.edit().putString("participant_ID",String.valueOf(newParticipantID)).commit(); } }}
该服务使用ApplicationContext初始化SyncAdapter:
public class SyncService extends Service { private static final Object sSyncAdapterLock = new Object(); private static SyncAdapter sSyncAdapter = null; @OverrIDe public voID onCreate() { synchronized (sSyncAdapterLock) { if (sSyncAdapter == null) { sSyncAdapter = new SyncAdapter(getApplicationContext(),false); } } } @OverrIDe public IBinder onBind(Intent intent) { return sSyncAdapter.getSyncAdapterBinder(); }}
Activity中的一个静态函数,由Activity检查SharedPreference.这不返回在SyncAdapter中提交的值,而是返回旧值. (我的设置活动和其他活动也使用旧值.):
public static boolean isUserLoggedIn(Context ctx) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx); int participantID = Integer.parseInt(prefs.getString("participant_ID","0")); LOGD("dg_Utils","isUserLoggedIn.participantID: " + participantID);// Todo if (participantID <= 0) { ctx.startActivity(new Intent(ctx,LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_top)); return false; } return true;}
更新:当我完全关闭应用程序(从运行的应用程序中滑动)时,我获得了新的值.我还有一个SharedPreferencechangelistener,当更改首选项时,它不会被触发.
private final SharedPreferences.OnSharedPreferencechangelistener mParticipantIDPrefchangelistener = new SharedPreferences.OnSharedPreferencechangelistener() { public voID onSharedPreferenceChanged(SharedPreferences prefs,String key) { if (key.equals("participant_ID")) { LOGI(TAG,"participant_ID has changed,requesting to restart the loader."); mRestartLoader = true; } }};@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // subscribe to the participant_ID change Lister final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); PARTICIPANT_ID = Integer.parseInt(prefs.getString("participant_ID","0")); prefs.registerOnSharedPreferencechangelistener(mParticipantIDPrefchangelistener);}解决方法 好的,我通过@Titus的帮助,经过一些研究,拼凑了一个解决我的问题的自己.
没有更新相同上下文的DefaultSharedPreferences的原因是我已经指定了SyncService在AndroIDManifest.xml中的自己的进程中运行(见下文).因此,从AndroID 2.3开始,其他进程被阻止访问更新的SharedPreferences文件(参见this answer和Context.MODE_MULTI_PROCESS
上的AndroID文档).
<service androID:name=".sync.SyncService" androID:exported="true" androID:process=":sync" tools:ignore="ExportedService" > <intent-filter> <action androID:name="androID.content.SyncAdapter" /> </intent-filter> <Meta-data androID:name="androID.content.SyncAdapter" androID:resource="@xml/syncadapter" /> </service>
所以在SyncAdapter和我的应用程序的UI进程中访问SharedPreferences时,我必须设置MODE_MulTI_PROCESS.因为我在整个应用程序中广泛使用了PreferenceManager.getDefaultSharedPreferences(Context),所以我编写了一个实用程序方法,并用此方法替换了PreferenceManager.getDefaultSharedPreferences(Context)的所有调用(见下文).首选项文件的默认名称是硬编码的,并从the Android source code和this answer派生.
public static SharedPreferences getDefaultSharedPreferencesMultiProcess( Context context) { return context.getSharedPreferences( context.getPackagename() + "_preferences",Context.MODE_PRIVATE | Context.MODE_MulTI_PROCESS);}总结
以上是内存溢出为你收集整理的Android在SyncAdapter中提交的SharedPreference在Activity中未更新?全部内容,希望文章能够帮你解决Android在SyncAdapter中提交的SharedPreference在Activity中未更新?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)