无法调用共享Preferences类的函数(构造函数)来维护Session

时间:2017-10-26 18:53:47

标签: java android session

我创建了一个用于存储会话的类因此,如果用户没有注销,我可以绕过登录屏幕但是我不知道为什么我无法访问该类的功能(构造函数)。

package pk.com.edu.farrukhabbas.hcs.Combined;
import java.util.HashMap;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;

import pk.com.edu.farrukhabbas.hcs.Doctor.DoctorSignInActivity;

class SessionManager {
// Shared Preferences
SharedPreferences pref;

// Editor for Shared preferences
Editor editor;

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Sharedpref file name
private static final String PREF_NAME = "Farrukh Abbas" , IS_LOGIN = "" , KEY_NAME = "", KEY_EMAIL = "", KEY_USERDESIG = "", KEY_ID = "", KEY_MOBILE = "", KEY_CNIC = "",
        KEY_DOB = "", KEY_GENDER = "", KEY_PASSWORD = "", KEY_LAT = "", KEY_LONG = "";


// Constructor
public SessionManager(Context context) {
    this._context = context;

    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

    editor = pref.edit();
}
public void session()
{

}

public void createPatientLoginSession(String USERDESIG, String ID, String NAME, String MOBILE, String CNIC, String DOB, String EMAIL, String GENDER, String PASSWORD)
{
    // Storing login value as TRUE
    editor.putBoolean(IS_LOGIN, true);

    editor.putString(KEY_NAME, NAME);

    editor.putString(KEY_USERDESIG, USERDESIG);

    editor.putString(KEY_CNIC, CNIC);

    editor.putString(KEY_ID, ID);

    editor.putString(KEY_DOB, DOB);

    editor.putString(KEY_MOBILE, MOBILE);

    editor.putString(KEY_GENDER, GENDER);

    editor.putString(KEY_EMAIL, EMAIL);


    editor.putString(KEY_PASSWORD, PASSWORD);



    // commit changes
    editor.commit();
}


/**
 * Quick check for login
 * **/
// Get Login State
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
}


public void checkLogin(){
    // Check login status
    if(!this.isLoggedIn()){
        // user is not logged in redirect him to Login Activity
        if(KEY_USERDESIG.equals("Doctor")) {
            Intent i = new Intent(_context, DoctorSignInActivity.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
            _context.startActivity(i);
        }
        else
        {
            Intent i = new Intent(_context, SignInActivity.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
            _context.startActivity(i);


        }
    }

}

/**
 * Clear session details
 * */
public void logoutUser(){
    // Clearing all data from Shared Preferences

    String userDeignation = KEY_USERDESIG;

    editor.clear();
    editor.commit();

    if(userDeignation.equals("Doctor")) {
        Intent i = new Intent(_context, DoctorSignInActivity.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
        _context.startActivity(i);
    }
    else
    {
        Intent i = new Intent(_context, SignInActivity.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
        _context.startActivity(i);

    }


}

}

这是代码来自我的主要调用,我在其中创建对象并调用其构造函数

SessionManager session;
   session = new SessionManager((zzw) getApplicationContext());
   session.createPatientLoginSession(userDesignation, user_Id , user_Name 
   ,user_Mobile ,user_Cnic, user_Dob , user_Email ,user_Gender, 
   user_Password);

enter image description here

1 个答案:

答案 0 :(得分:0)

类SessionManager是类中的Bilt 编译器正在寻找函数

createPatientLoginSession()

在那个内置类中,因为它具有高优先级 更改班级的名称" SessionManager"任何其他名称将解决问题