4

I have two app one app is named as "A" and another app named as "B" ,scenario is something like that app B is integrated in app A and App B has a class called CLoginSessionManagement which contain code for user data stored in that class.What I want in App "A" to access user data from CLoginSessionManagement and make condition if user is login or not. How can I do that?

Code for CLoginSessionManagement:

public class CLoginSessionManagement {
    // User name (make variable public to access from outside)
    public static final String s_szKEY_MOBILE = "agentcode";
    // Email address (make variable public to access from outside)
    public static final String s_szKEY_PASSWORD = "pin";
    // Sharedpref file name
    private static final String s_szPREF_NAME = "LoginData";
    // All Shared Preferences Keys
    private static final String s_szIS_LOGIN = "IsLoggedIn";
    private final SharedPreferences m_LOGIN_PREF;
    private final Editor m_EDITOR;
    private final Context m_CONTEXT;


    // Constructor
    @SuppressLint("CommitPrefEdits")
    public CLoginSessionManagement(Context m_CONTEXT) {
        this.m_CONTEXT = m_CONTEXT;
        m_LOGIN_PREF = m_CONTEXT.getSharedPreferences(s_szPREF_NAME, Context.MODE_PRIVATE);
        m_EDITOR = m_LOGIN_PREF.edit();
    }


    // Registeration Session Management....
    public void setLoginData(String mobile, String pin) {
        m_EDITOR.putBoolean(s_szIS_LOGIN, true);
        m_EDITOR.putString(s_szKEY_MOBILE, mobile);
        m_EDITOR.putString(s_szKEY_PASSWORD, pin);
        m_EDITOR.commit();
    }

    /**
     * checkLogin() session wil check user Login status
     * If false it will redirect user to Login page
     * Else won't do anything
     */
    public boolean checkLogin() {
        if (!isLogin()) {
            Intent i = new Intent(m_CONTEXT, CMainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            m_CONTEXT.startActivity(i);
            return true;
        }
        return false;
    }

    /**
     * Get stored Login session data
     */
    public HashMap<String, String> getLoginDetails() {
        HashMap<String, String> user = new HashMap<>();
        // user name
        user.put(s_szKEY_MOBILE, m_LOGIN_PREF.getString(s_szKEY_MOBILE, null));
        // user email id
        user.put(s_szKEY_PASSWORD, m_LOGIN_PREF.getString(s_szKEY_PASSWORD, null));
        // return user
        return user;
    }

    public boolean isLogin() {
        return m_LOGIN_PREF.getBoolean(s_szIS_LOGIN, false);
    }

    /**
     * Clear session details
     */
    public void logoutUser() {
        // Clearing all data from Shared Preferences
        m_EDITOR.clear();
        m_EDITOR.commit();
        @SuppressWarnings("UnusedAssignment") final AppCompatActivity activity = (AppCompatActivity) m_CONTEXT;
        Intent i = new Intent(m_CONTEXT, CLoginScreen.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        m_CONTEXT.startActivity(i);
        ((AppCompatActivity) m_CONTEXT).finish();

    }
}

m_oLoginSession = new CLoginSessionManagement(CLoginScreen.this);// object creatiion of Login session.
comrade
  • 4,590
  • 5
  • 33
  • 48
vishwas
  • 251
  • 1
  • 3
  • 16

3 Answers3

0

I just have a look to android dev docs and it states that SharedPreferences is private to your application. You may try content provider:

Android provides a way for you to expose even your private data to other applications — with a content provider.

Ref links:

Tommy Kam
  • 21
  • 2
0

Since SharedPreferences are protected by android-os you have to store and load the content at a file directory that both apps can read/write.

The trick to do this replace the android-sandbox Context with your own code.

Instead of calling MyActivity.this.getSharedPreferences(...) you use

Context context = new PublicPreferencesContext(MyActivity.this);
context.getSharedPreferences(...);

Here is where all the magic happens:

class PublicPreferencesContext extends ContextWrapper {
    public PublicPreferencesContext(Context base) {
        super(base);
    }

    // overwrite the shared-preferences specific functions here
    // getSharedPreferences, ...
}

I have done something similar to change the location where the database is stored wich works very well. Doing this whith SharedPreferences should work as well.

If this works for you please let us know and give us the complete sourcecode for PublicPreferencesContext

Community
  • 1
  • 1
k3b
  • 14,517
  • 7
  • 53
  • 85
-1

SharedPreferences only exist itself package You can try learn :

  1. content provider
  2. aidl (IPC)
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95