我如何使我的应用程序成为记住/保存我的应用程序用户获得我的应用程序的硬币?

我如何使我的应用程序成为记住/保存我的应用程序用户获得我的应用程序的硬币?

问题描述:

我希望我的应用记住或保存用户在我的应用中赚取的硬币数量,以便每当他们再次打开我的应用时,他们都能看到自己的硬币,而不是0。

I want my app to remember or save the number of coins that users earn in my app so that whenever they open my app again they can see their coins instead of a 0.

您应该在Android上使用共享首选项。

You should use shared preferences on Android.

我写下了共享应用程序的完整功能示例偏好。

I write down a fully functional example of an application of a shared preferences. This one is taken from my project (link down the code).

首先,您应该创建一个类(假设您的硬币将用于更多活动) 。
这个叫做 SessionManager.java

First, you should create a class (supposing that your "coins" will be used in more activities). This one is called SessionManager.java:

    public 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 = "FoodAdvisorPref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";
    public static final String KEY_AZIENDA = "aziendaName";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_NAME = "name";
    public static final String KEY_SECOND_NAME = "second_name";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_ID = "id";
    public static final String KEY_PHOTO = "photo";



    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }


    /**
     * Create login session
     * */
    public void createLoginSession(String azienda,String name,String second_name, String email,String description,String id,String photo){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

        editor.putString(KEY_NAME, name);
        editor.putString(KEY_EMAIL, email);
        editor.putString(KEY_AZIENDA,azienda);
        editor.putString(KEY_SECOND_NAME,second_name);
        editor.putString(KEY_DESCRIPTION,description);
        editor.putString(KEY_PHOTO,photo);
        editor.putString(KEY_ID,id);
        // commit changes
        editor.commit();
    }

    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            // user is not logged in redirect him to Login Activity
            Intent i = new Intent(_context, LoginActivity.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);
        }

    }


//session data like hash
    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));
        user.put(KEY_AZIENDA, pref.getString(KEY_AZIENDA, null));
        user.put(KEY_SECOND_NAME, pref.getString(KEY_SECOND_NAME, null));
        user.put(KEY_DESCRIPTION, pref.getString(KEY_DESCRIPTION, null));
        user.put(KEY_ID, pref.getString(KEY_ID, null));
        user.put(KEY_PHOTO,pref.getString(KEY_PHOTO,null));

        return user;
    }

//close session
    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();

        // After logout redirect user to Loing Activity
        Intent i = new Intent(_context, LoginActivity.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);

        // Starting Login Activity
        _context.startActivity(i);
    }

    // Get Login State
    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}

要在其他活动中使用此类

  SessionManager session; //as global variable if you want
  session = new pro.rane.foodadvisor.SessionManager(getApplicationContext()); //inside create view
 session.checkLogin();
 // get user data from session
 HashMap<String, String> user = session.getUserDetails();
 // name example
 String name = user.get(SessionManager.KEY_NAME);
 // email example
 String email = user.get(SessionManager.KEY_EMAIL);

这里是我的项目的来源。

此处是教程指向的实用链接。

Here is a practical link by tutorials point.

此处是Android开发人员。

Here is the official guide for Android developers.