登录会话 - 活动 [英] Login Session - Activities

查看:143
本文介绍了登录会话 - 活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序登录。我想知道,设定一个全球性会议或同等学历,我可以从任何活动是指,让他们知道,如果一个用户登录的最佳途径。此外,让其他的活动可以获取用户ID做数据库事务。

I have a login in my application. I want to know the best way to set a global session or equivalent that I can refer to from any activity so that they will know if a user is logged in. Also, so that other activities can get the user id to do database transactions.

我不希望使用putExtra()我将不得不这样做的每一个意图我建立在整个应用程序。

I don't want to use putExtra() as I will have to do it for every Intent I build across the application.

推荐答案

您可以创建一个可以作为全球性状态保持一个应用程序类。

You can create a Application class which can be used as global state holder.

下面有一个样本code:

Heres a sample code:

public class SampleApplication extends Application {

private static String username;
private static String password;

@Override
public void onCreate() {
    super.onCreate();
    username="";
    password="";
}

public static String getUsername() {
    return username;
}

public static void setUsername(String username) {
    SampleApplication.username = username;
}

public static String getPassword() {
    return password;
}

public static void setPassword(String password) {
    SampleApplication.password = password;
}


}

声明静态方法和变量后,你应该定义你的应用类你AndroidManifest.xml中使用安卓的应用程序的名称属性,以便确定你的类名称的标签。

after declaring static methods and variables you should define you application class in you AndroidManifest.xml use the android:name attribute of the application tag in order to define you class name.

继承人的样本code:

heres a sample code:

<application 
    android:name=".SampleApplication"
    android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SampleApp"
              android:label="@string/app_name"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

您的应用程序类将共享所有的生命周期事件作为活动的,将被摧毁时,应用程序退出。

You application class will share all the life cycle events as that of Activity and will get destroyed when application exits.

您可以从任何活动或服务的静态getter和setter访问你的应用程序变量。

You can access you application variables with static getters and setters from any activity or service. :

SampleApplication.setUsername("");
String currentUserName=SampleApplication.getUsername();
SampleApplication.setPassword("");
String currentPassword=SampleApplication.getPassword();

您也可以,而不是去为单例类选择正常类应用

You can also rather than going for singleton class opt for normal class of Application

编辑同一个例子: 公共类SampleApplication扩展应用{

Editing the same example: public class SampleApplication extends Application {

private String username;
private String password;

@Override
public void onCreate() {
    super.onCreate();
    username="";
    password="";
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    SampleApplication.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    SampleApplication.password = password;
}


}

您可以访问这些:

((SampleApplication )getApplication()).getUsername();

这篇关于登录会话 - 活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆