更好的方式携带用户信息Android应用程序

时间:2016-03-14 09:43:14

标签: android android-intent user-data

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

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

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

4 个答案:

答案 0 :(得分:0)

我通常使用currentUser单例制作一个User类。在您的情况下,它可能只有一个用户名,但是您可以很好地进行扩展。您可以通过检查currentUser是否为空来检查未登录的情况。

答案 1 :(得分:0)

将Sharedpreferance用于商店用户信息。

这是Helper Class of Sharedreferance。

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");

我希望它能帮到你

答案 2 :(得分:0)

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

"""
This program demonstrates that you can write the bit operators AND, OR and XOR
in terms of the subtract and branch if equal or lower (subleq) operator.
https://en.wikipedia.org/wiki/One_instruction_set_computer

C.M. Punter 2016
"""
import random


def my_sub(a: int, b: int) -> int:
    return a - b


def my_leq(a: int, b: int) -> bool:
    return a <= b


# now we write all other functions in terms of the above two functions

def my_add(a: int, b: int) -> int:
    return my_sub(a, my_sub(0, b))


def my_mul(a: int, b: int) -> int:

    c = 0

    while my_leq(1, b):
        c = my_add(c, a)
        b = my_sub(b, 1)

    return c


def my_div(a: int, b: int) -> int:

    c = 0

    while my_leq(b, a):
        c = my_add(c, 1)
        a = my_sub(a, b)

    return c


def my_mod(a: int, b: int) -> int:

    while my_leq(b, a):
        a = my_sub(a, b)

    return a


def my_and(a: int, b: int) -> int:

    c = 0
    d = 1

    while my_leq(1, my_mul(a, b)):

        # we look at the first bit of a and b by checking if the number is odd or even
        # for this we use the modulo operator (a mod 2 and b mod 2)
        # this results in a 1 if the number is odd (which means the first bit is 1)
        # and 0 if the number is even (which means the first bit is 0)
        # multiplying those two numbers gives the and-operator for the first bit of a and b
        # the other bits are handled by bit-shifting a and b
        # this is equivalent to multiplying (left-shift) or dividing (right-shift) by 2

        c = my_add(c, my_mul(my_mul(my_mod(a, 2), my_mod(b, 2)), d))
        d = my_mul(d, 2)
        a = my_div(a, 2)
        b = my_div(b, 2)

    return c


def my_or(a: int, b: int) -> int:

    c = 0
    d = 1

    while my_leq(1, my_add(a, b)):
        if my_leq(1, my_add(my_mod(a, 2), my_mod(b, 2))):
            c = my_add(c, d)

        d = my_mul(d, 2)
        a = my_div(a, 2)
        b = my_div(b, 2)

    return c


def my_xor(a: int, b: int) -> int:

    c = 0
    d = 1

    while my_leq(1, my_add(a, b)):
        e = my_add(my_mod(a, 2), my_mod(b, 2))
        if my_leq(e, 1):
            c = my_add(c, my_mul(e, d))

        d = my_mul(d, 2)
        a = my_div(a, 2)
        b = my_div(b, 2)

    return c


def test(a: int, b: int):
    assert a & b == my_and(a, b)
    assert a | b == my_or(a, b)
    assert a ^ b == my_xor(a, b)

for i in range(1000):
    test(random.randint(0, 1024), random.randint(0, 1024))

答案 3 :(得分:0)

像这样创建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();