在PHP中持有用户ID的会话等效于什么?

时间:2018-11-20 22:36:26

标签: android firebase

在PHP中,当用户登录(谈论简单的Web应用程序)时,我将其用户ID保留在保存在所有页面中的会话中。 假设我已登录我的应用程序(例如,使用Firebase),如何跟踪当前用户ID?我想了解它在Android上的工作原理。所有片段/活动中是否都存在一些全局变量?

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找Firebase身份验证。用户从设备登录一次,然后Firebase维护他们的会话。

登录可以通过pre-built UI完成,也可以通过构建自己的UI并制作calls to the API来完成。

对于上述任何一种,您都可以在每个活动中pick up the existing user进行以下操作:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create an ObservableList to hold our Cars
        ObservableList<Car> carsList = FXCollections.observableArrayList();

        // For our sample, let's use a FlowPane to display all of our buttons. We will add new buttons to this FlowPane
        // automatically as new Cars are added to carsList
        FlowPane flowPane = new FlowPane();
        flowPane.setHgap(10);
        flowPane.setVgap(5);
        flowPane.setAlignment(Pos.TOP_CENTER);

        // Create a ListChangeListener for our carsList. This allows us to perform some actions whenever an item is added
        // to or removed from the list. For our example, we will only do something when a new Car is added.
        carsList.addListener(new ListChangeListener<Car>() {
            @Override
            public void onChanged(Change<? extends Car> c) {

                System.out.println(carsList.size());
                // Get the first change
                c.next();

                // If an item was added to the list...
                if (c.wasAdded()) {

                    // Create a new button and add it to the FlowPane
                    // The Change (c) provides access to a List of items that were added during this change. Since we
                    // are only going to add one Car at a time, we only need to get the first item from the AddedSubList
                    Button button = new Button(c.getAddedSubList().get(0).getName());
                    button.setGraphic(c.getAddedSubList().get(0).getIcon());
                    button.setOnAction(event -> {
                        // The code to be executed when this button is clicked goes here
                    });

                    // Add the button to our FlowPane
                    flowPane.getChildren().add(button);
                }

            }
        });

        // Now we need a Button that will add a new car to the List
        Button button = new Button("Add Car");
        button.setOnAction(event -> {
            // We'll just add a random car to the carsList
            carsList.add(new Car("Car #" + (carsList.size() + 1), new ImageView("icon.png")));
        });

        // Add our FlowPane and Button to the root layout
        root.getChildren().addAll(button, flowPane);

        primaryStage.setScene(new Scene(root, 550, 250));
        primaryStage.show();
    }
}

class Car {

    private final String name;
    private final ImageView icon;

    public Car(String name, ImageView icon) {
        this.name = name;
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public ImageView getIcon() {
        return icon;
    }
}

答案 1 :(得分:1)

有关更一般的解决方案(如果您不使用Firebase),请考虑使用SharedPreferences

https://developer.android.com/reference/android/content/SharedPreferences

请注意,这还将在重新启动应用程序时保留用户ID。

使用非常简单,这是一个好处:

//Save value
getSharedPreferences().edit().putString("my_user_id", userId).apply();

//Retrieve value
String userId = getSharedPreferences().getString("my_user_id", default_value);