0

My 2 android applications communicate throught a AIDL file :

Application A <== AIDL ==> Application B (service)

Application A calls methods of Application B (service) that returns some JSON information. However, If user is not logged in application B, null is returned.

(service in application B)

//first : get info
SharedPreferences mPrefs = getSharedPreferences(Consts.SP_NAME, MODE_PRIVATE);
String userId = mPrefs.getString(Consts.PREFS_USER_ID, "");
String userPass = mPrefs.getString(Consts.PREFS_USER_PASS, "");

//then check is valid (remote server)
if (dao.exist (userId, userPass)){
 return "MY_JSON_INFO" ;
}else{
 return false;
}

If null is returned, "login" activity in Application B is launched from Application A.

(Aplication A)

if(json == null){
  Intent intent = new Intent("jp.app.LOGIN_ACTIVITY");
  startActivity(intent);
}

User log in (Application B), data are saved in SharedPreferences (Application B) and activity is closed (back to Application 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();

When re-trying to call methods of the Application B, login information saved few seconds ago are not available and null is returned.

I have tried without success with prefsEditor.apply() .

If I restart application B, login data will be loaded....

Please let me know if further information are needed. Thank you

johann
  • 1,115
  • 8
  • 34
  • 60

3 Answers3

1

After a long struggle, I have at last managed to get my updated preferences.

When calling getSharedPreferences, I use Context.MODE_MULTI_PROCESS flag.

MODE_MULTI_PROCESS

Thank you.

johann
  • 1,115
  • 8
  • 34
  • 60
0

By reading your comment i can give you a quick solution:

The easier way is to simply terminate your activity (A) before calling the login activity inside your service.

if(json == null){
  Intent intent = new Intent("jp.app.LOGIN_ACTIVITY");
  startActivity(intent);
  this.finish();
}

So that, when you fill in the login form and call back the activity A it will read the new shared preferences!

iGio90
  • 3,251
  • 7
  • 30
  • 43
  • If I first login with B application and then start A application, it works well (I mean JSON is returned). – johann Nov 26 '13 at 06:45
  • and your issue is: you launch A. if there isn't any sharedprefs with login data it goes to B for login. Once logged in it move back to A but there isn't any data loaded until you kill and reopen A. right? – iGio90 Nov 26 '13 at 06:50
  • Login information is checked in the B application (service). If login information is present and correct, it will return JSON info. If not, it will return null. In this case, login activity (application B) will be called – johann Nov 26 '13 at 07:27
  • I understand this, but once logged in B, it move back to A. once moved back to A it doesn't get any data until you restart A. is that right? – iGio90 Nov 26 '13 at 07:29
  • perfectly right ! in other words, I cannot get updated data. But if I restart A application, I can login. – johann Nov 26 '13 at 07:36
  • Thank you for your reply. Actually When log in information is validated, Login activity is closed and user goes back to the A application... So I cannot "finish" it. – johann Nov 27 '13 at 01:12
  • I made a mistake in my previous message.. I can't can any data until I restart B application (not A) – johann Nov 27 '13 at 01:48
0

Since you need to access the shared preference of other application.

You should try :: Android: Retrieving shared preferences of other application

Community
  • 1
  • 1
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64