简体   繁体   中英

User Login and Password: Database Conformation

I am creating an app that requires a user to login before enter the main app. When he hits the "login" button, it should check to make sure that the email/username and password are correct before allowing the user into the app or else give them an error. I have been having a hard time trying to figure out how to do this. Here is my main_activity pages which show the main login screen.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="vertical">

 <!--  Email Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="@string/Email"/>

                <EditText
                    android:id="@+id/Email_enter"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dip"
                    android:layout_marginTop="5dip"
                    android:hint="@string/Email_enter"
                    android:singleLine="true" 
                    android:inputType="textEmailAddress"/>

                <!--  Password Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="@string/edit_message2"/>

      <EditText
          android:id="@+id/Password_enter"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:hint="@string/Password_enter"
          android:inputType="textPassword"
          android:singleLine="true" />

      <!-- Login button -->

      <Button android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="@string/Login"
            android:onClick="sendMessage"/>

      <Button
          android:id="@+id/query"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Query"
          android:onClick="View arg0"/>

Please ignore the "query" button for now. Here is the java file:

    package com.example.awesomefilebuilder;


    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    public class MainActivity extends Base_Activity {

    public final static String EXTRA_MESSAGE = "com.example.awesomefilebuilder.MESSAGE";
    public final String TAG = "MainActivity";
    public final String edit_message ="Username";
    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "comments_table";
    public static final String C_ID = "_id";
    public static final String NAME = "name";
    public static final String COMMENT = "comment";
    public static final String EMAIL = "email";
    public static final String TIME = "time";
    public static final String PASSWORD = "password";
    public static final int VERSION = 1;

    View view;
    SQLiteDatabase db;
    DbHelper dbhelper;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        DbHelper dbhelper = new DbHelper(getApplicationContext());
        db = dbhelper.getWritableDatabase();
        view = inflater.inflate(R. layout.activity_main, container,false);


            ContentValues cv = new ContentValues();


        cv.put(DbHelper.NAME, NAME);
        cv.put(DbHelper.COMMENT, COMMENT);
        cv.put(DbHelper.EMAIL, EMAIL);
        cv.put(DbHelper.PASSWORD, PASSWORD);
        db.insert(DbHelper.TABLE_NAME, null, cv);


    Button query = (Button) view.findViewById(R.id.query);
    query.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            String [] columns = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.EMAIL};

            Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
            cursor.moveToFirst();

                while(cursor.moveToNext())
                {
                    String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
                    String comment = cursor.getString(cursor.getColumnIndex(DbHelper.COMMENT));
                    String password = cursor.getString(cursor.getColumnIndex(DbHelper.PASSWORD));
                    String email = cursor.getString(cursor.getColumnIndex(DbHelper.EMAIL));

                    Toast.makeText(getApplicationContext(), "Name = "+ name +"/nComment= "+ comment,Toast.LENGTH_SHORT).show();
                }

                cursor.close();




        }

    });
        return view;


    }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate" );

}

@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass

    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}


/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, HomePageMain.class);
    EditText editText = (EditText) findViewById(R.id.Email_enter);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    Log.i(TAG, "sendMessage" );
    }

};

When the user hits the Login button, I want the app to check the data before allowing them in or giving an error. How do I do this here? After that, if the data does check out correctly, I want the app to send the user to the next main page called HomePageMain.class.

If any other pages are needed, please let me know. Thanks in advance! I really apperciate it.

Its a broad question. What you might need to do is you would have to detect when user is running the app for the first time. On first time login you can ask the user to enter a username and Password. Now the simplest ( but might not be the best) way is to save it in SharedPreferences . Then when user come to the app next time, you can check it from the SharedPreference and if login is correct then continue, else show an error.

Inside your sendMessage function you can first check if anything is stored in sharepreference with a particular key that you've used for username and password. If its not saved then user is coming for first time. Then strat a new Intent where you can save it. If there is some saved things, then you can extract thme from shared preference and compare the values against the entered values.

Now its not a good idea to save password in SharedPreference as on rooted devices those can be accessed. You can use hashing/encrypting on the password.

See remember password in android app

SharedPreferences for login in android and

Best option to store username and password in android app

for the code sniplets and other logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM