【问题标题】:Better way of carrying user information android application更好的方式携带用户信息的android应用
【发布时间】:2016-03-14 09:43:13
【问题描述】:

我正在开发一个需要用户帐户的 android 应用程序。目前,我将用户名作为附件与每个新 Intent 一起传递,如下所示:

 Intent child = new Intent(Login.this, Child_Home.class);
    child.putExtra("username", username);
    startActivity(child);

然后在每个新活动中阅读它。但是,我知道这效率低下。还有其他方法可以做到这一点吗?我只需要用户名,因为它是唯一的。

【问题讨论】:

    标签: android android-intent user-data


    【解决方案1】:

    我通常使用 currentUser 单例创建一个 User 类。在您的情况下,它可能只有一个用户名,但是您已经为扩展做好了准备。您可以通过检查 currentUser 是否为空来检查未登录的情况。

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • @Mike 是的,它确实提供了一个答案——它告诉了一个要使用的模式。审稿时请多加注意
    【解决方案2】:

    使用 Sharedpreferences 存储用户信息。

    这是共享引用的辅助类。

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    public class SharedPreference {
    
        private static SharedPreference   sharedPreference;
        public static final String PREFS_NAME = "AOP_PREFS";
        public static final String PREFS_KEY = "AOP_PREFS_String";
    
    
        public static SharedPreference getInstance()
        {
            if (sharedPreference == null)
            {
                sharedPreference = new SharedPreference();
            }
            return sharedPreference;
        }
    
        public SharedPreference() {
            super();
        }
    
        public void save(Context context, String text , String Key) {
            SharedPreferences settings;
            Editor editor;
    
            //settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
            editor = settings.edit(); //2
    
            editor.putString(Key, text); //3
    
            editor.commit(); //4
        }
    
        public String getValue(Context context , String Key) {
            SharedPreferences settings;
            String text = "";
          //  settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            text = settings.getString(Key, "");
            return text;
        }
    
        public void clearSharedPreference(Context context) {
            SharedPreferences settings;
            Editor editor;
    
            //settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            editor = settings.edit();
    
            editor.clear();
            editor.commit();
        }
    
        public void removeValue(Context context , String value) {
            SharedPreferences settings;
            Editor editor;
    
            settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            editor = settings.edit();
    
            editor.remove(value);
            editor.commit();
        }
    }
    

    复制这个类。

    您可以像这样存储用户信息。

    SharedPreference.getInstance().save(LoginScreen.this, Username , "Key_for_Username");
    

    你可以像这样在整个应用程序中获得价值。

    String Username = SharedPreference.getInstance().getValue(context,"Key_for_Username");
    

    如果你想清除所有值而不是

    SharedPreference.getInstance().clearSharedPreference(ActivityContext);
    

    如果你想删除特定的值而不是

    SharedPreference.getInstance().removeValue(context , "key_value");
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      我认为您应该使用 Shared Preference 来保存登录凭据和其他信息。下面的代码将帮助您了解如何使用共享首选项。使用这种类来存储数据,您可以在应用程序的任何地方访问这些数据。

      public class SessionLogin {
      
          SharedPreferences pref;
          SharedPreferences.Editor editor;
          Context context;
          int PRIVATE_MODE = 0;
      
          private static final String PREF_NAME = "MyAppPref";
          private static final String IS_LOGIN = "IsLoggedIn";
          public static final String KEY_NAME = "name";
          public static final String KEY_EMAIL = "email";
      
          public SessionLogin(Context context){
              this.context = context;
              pref = this.context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
              editor = pref.edit();
          }
      
          public void createLoginSession(String name, String email){
      
              editor.putBoolean(IS_LOGIN, true);
              editor.putString(KEY_NAME, name);
              editor.putString(KEY_EMAIL, email);
              editor.commit();
          }
      
          public void checkLogin(){
      
              if(!this.isLoggedIn()){
      
                  Intent i = new Intent(context, ActivityHome.class);
                  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(i);
              }
          }
      
          public HashMap<String, String> getUserDetails(){
      
              HashMap<String, String> user = new HashMap<String, String>();
              user.put(KEY_NAME, pref.getString(KEY_NAME, null));
              user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
              return user;
          }
      
          public void logoutUser(){
      
              editor.clear();
              editor.commit();
      
              Intent i = new Intent(context, ActivitySignIn.class);
              i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);
          }
      
          public boolean isLoggedIn(){
              return pref.getBoolean(IS_LOGIN, false);
          }
      }
      

      【讨论】:

        【解决方案4】:

        像这样创建 Singleton 类并将用户名存储在那里,然后在应用程序的任何位置重用该用户名。

        public class SharedInstance {
        
        private static SharedInstance singleton = new SharedInstance( );
        private String username;
        
        /* A private Constructor prevents any other
         * class from instantiating.
         */
        private SharedInstance(){ }
        
        /* Static 'instance' method */
        public static SharedInstance getInstance( ) {
            if (singleton == null){
                singleton = new SharedInstance();
            }
            return singleton;
        }
        
        public String getUserName() {
            return username;
        }
        
        public void setUserName(String username) {
            this.username = username;
        }
        
        }
        

        现在用于在应用程序的任何位置设置用户名数据

        SharedInstance.getInstance().setUserName(username);

        以及在应用程序的任何位置获取用户名数据

        SharedInstance.getInstance().getUserName();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-03
          • 1970-01-01
          • 2010-09-11
          • 2013-06-19
          • 2019-04-07
          • 1970-01-01
          相关资源
          最近更新 更多