代码之家  ›  专栏  ›  技术社区  ›  Prakash Kumar

如何引导用户进行其特定活动?

  •  0
  • Prakash Kumar  · 技术社区  · 7 年前

    我正在构建一个应用程序,当用户第一次安装它时,在MainActivity(Launcher活动)中,应用程序会询问他是什么类型的用户(比如说司机或骑手)?根据用户选择,他被引导到相应的登录屏幕。用户登录后,他将指向HomeActivity。如果他没有注销就关闭了应用程序,下次打开应用程序时,他应该直接看到HomeActivity(根据他选择的用户类型)。对于一个用户,MainActivity中的代码如下:

    @Override
    protected void onStart() {
        super.onStart();
    
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseAuth mAuth = FirebaseAuth.getInstance();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser != null) {
            sendToStart();
    
        }
    }
    
    private void sendToStart() {
    
        Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
        startActivity(startIntent);
        finish();
    }
    

    我是Android和Firebase的新手。我不知道这对于两种类型的用户应该怎么做。

    4 回复  |  直到 5 年前
        1
  •  1
  •   Peter Haddad    7 年前

    创建一个登录屏幕,用户可以在该屏幕中选择工作类型(两个按钮或单选按钮)。

    如果他选择了Driver,则使用他编写的信息在数据库中创建一个Driver节点,并将其引导到主活动:

    FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Drivers").child(user.getUid());
    ref.child("name").setValue(name);
    Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
    startActivity(startIntent);
    finish();
    

    对骑手也要这样做。


    如果是用户,则在不注销的情况下关闭应用程序。在应打开的第一个活动(例如:splash活动)中,检查是否有当前用户,并检查用户类型:

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
    DatabaseReference driver = FirebaseDatabase.getInstance().getReference().child("Drivers"); 
    DatabaseReference rider = FirebaseDatabase.getInstance().getReference().child("Riders");
    
      if (user != null) {
        driver.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() { 
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) { 
                        Intent i = new Intent(SplashActivity.this, DriverHomeActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        Intent intent = new Intent(SplashActivity.this, RiderHomeActivity.class);
                        startActivity(intent);
                        finish();
                      }
                @Override
              public void onCancelled(DatabaseError databaseError) {
    
                   }
               });
    

    在splash活动中,它将检查是否有当前用户(如果用户已登录) user.getUid() 将返回当前用户ID,然后检查此用户ID是否在 Drivers 节点,然后它将他引导到驾驶员的主页活动。

        2
  •  0
  •   Shalu T D roplacebo    7 年前

    您的问题可以通过使用 SharedPreference 机械装置 您可以在此处保持登录状态 共享引用 如下:-。

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
    prefs.edit().putBoolean("login", true).commit();
    

    当用户无法注销并返回时,应使用 共享引用 变量并将用户移动到相应的活动。

    布尔loginStatus=首选项。getBoolean(“登录”,false);

    if (loginStatus) {
    // Move to Home activity
    } else {
    //Move to login screen
    }
    

    小文档 here

        3
  •  0
  •   Reaz Murshed vir us    7 年前

    你可以考虑 LauncherActivity 在您的android应用程序中,它将决定进一步启动哪个活动。A样品 启动器活动 可能如下所示。

    public class LauncherActivity extends AppCompatActivity {
    
        private boolean loggedIn = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            boolean isLoggedInAsDriver = getSharedPreferences.getBoolean("is_driver", false);
            startApplication(isLoggedInAsDriver);
            finish();
        }
    
        private void startApplication(boolean isLoggedInAsDriver) {
            Intent intent;
            if (isLoggedInAsDriver) {
                intent = new Intent(LauncherActivity.this, DriverActivity.class);
                startActivity(intent);
                finish();
            } else {
                intent = new Intent(LauncherActivity.this, RiderActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
    

    您的应用程序只能有一个启动器,因此请标记 启动器活动 作为android应用程序的启动器 AndroidManifest.xml 如下所示。

    <activity
        android:name=".LauncherActivity"
        android:label="Your app name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    现在你可能已经注意到我使用了一个标志(即。 isLoggedInAsDriver )从 启动器活动 。这是您需要存储在 SharedPreferences 当您可以确定用户是驾驶员还是骑手时。然后每次应用程序通过 启动器活动 ,检查存储的值并获取用户类型。根据用户类型,启动第二个活动。

        4
  •  0
  •   Aman Khandelwal    7 年前

    在清单中使用此代码 还有 您应该在数据库中使用用户ID保存用户类型,并在应用程序启动时首先检查此项,然后显示主页。

    <activity android:name=".MainActivity/>    
        <activity android:name=".DriverHomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>