Android:SharedReferences未更新
我的两个android应用程序通过AIDL文件进行通信: 应用程序A应用程序B(服务) 应用程序A调用应用程序B(服务)的方法,该方法返回一些JSON信息。但是,若用户未登录到应用程序B,则返回null (应用程序B中的服务) 如果返回null,则从应用程序A启动应用程序B中的“登录”活动Android:SharedReferences未更新,android,sharedpreferences,aidl,Android,Sharedpreferences,Aidl,我的两个android应用程序通过AIDL文件进行通信: 应用程序A应用程序B(服务) 应用程序A调用应用程序B(服务)的方法,该方法返回一些JSON信息。但是,若用户未登录到应用程序B,则返回null (应用程序B中的服务) 如果返回null,则从应用程序A启动应用程序B中的“登录”活动 SharedPreferences mPrefs = a.getSharedPreferences(Consts.SP_NAME, MODE_PRIVATE); Editor prefsEditor = mP
SharedPreferences mPrefs = a.getSharedPreferences(Consts.SP_NAME, MODE_PRIVATE);
Editor prefsEditor = mPrefs.edit();
prefsEditor.putString(Consts.PREFS_USER_ID, id);
prefsEditor.putString(Consts.PREFS_USER_PASS, pass);
prefsEditor.commit();
(应用A)
用户登录(应用程序B),数据保存在SharedReferences(应用程序B)中,活动关闭(返回应用程序A)
当您尝试调用应用程序B的方法时,几秒钟前保存的登录信息不可用,并返回null
我尝试使用prefsEditor.apply()但没有成功
如果我重新启动应用程序B,将加载登录数据
如果需要更多信息,请告诉我。
谢谢通过阅读您的评论,我可以为您提供一个快速解决方案: 更简单的方法是在调用服务中的登录活动之前,简单地终止活动(A)
if(json == null){
Intent intent = new Intent("jp.app.LOGIN_ACTIVITY");
startActivity(intent);
this.finish();
}
这样,当您填写登录表单并回调活动A时,它将读取新的共享首选项 因为您需要访问其他
应用程序的共享首选项
你应该试试看::经过长时间的努力,我终于获得了最新的偏好 调用GetSharedReferences时,我使用Context.MODE\u MULTI\u进程标志
谢谢。Editor prefsEditor=prefsEditor.commit();仅将这些行更改为\prefsEditor.commit();我已经更新了我的消息,sorryOk,请尝试danhnn提供的链接中的答案。谢谢。我不必共享首选项,因为应用程序B的服务可以访问应用程序B的SharedReferences。应用程序A不直接访问首选项。如果我首先使用B应用程序登录,然后启动应用程序,它工作正常(我的意思是返回JSON)。你的问题是:你启动了A。如果没有任何带有登录数据的SharedPref,它将转到B进行登录。登录后,将其移回A,但在杀死并重新打开A之前,不会加载任何数据。对吗?登录信息将在B应用程序(服务)中检查。如果登录信息存在且正确,它将返回JSON信息。否则,它将返回null。在这种情况下,登录活动(应用程序B)将被调用。我理解这一点,但一旦登录B,它将移回A。一旦移回A,它将在重新启动A之前不会获得任何数据。对吗?完全正确!换句话说,我无法获得更新的数据。但是,如果我重新启动一个应用程序,我可以登录。
SharedPreferences mPrefs = a.getSharedPreferences(Consts.SP_NAME, MODE_PRIVATE);
Editor prefsEditor = mPrefs.edit();
prefsEditor.putString(Consts.PREFS_USER_ID, id);
prefsEditor.putString(Consts.PREFS_USER_PASS, pass);
prefsEditor.commit();
if(json == null){
Intent intent = new Intent("jp.app.LOGIN_ACTIVITY");
startActivity(intent);
this.finish();
}