用户登录名和密码:数据库构造

时间:2013-08-24 22:44:48

标签: java android login passwords

我正在创建一个应用,要求用户在进入主应用之前登录。当他点击“登录”按钮时,它应该检查以确保电子邮件/用户名和密码是正确的,然后允许用户进入应用程序或者给他们一个错误。我一直在努力想弄清楚如何做到这一点。这是我的main_activity页面,显示主登录屏幕。

    <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"/>

请暂时忽略“查询”按钮。 这是java文件:

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

};

当用户点击“登录”按钮时,我希望应用程序在允许数据进入或发出错误之前检查数据。我怎么在这里这样做? 之后,如果数据检查正确,我希望应用程序将用户发送到名为HomePageMain.class的下一个主页。

如果需要任何其他页面,请告诉我。 提前致谢!我真的很感激它。

1 个答案:

答案 0 :(得分:0)

这是一个广泛的问题。你可能需要做的是你必须检测用户第一次运行应用程序的时间。首次登录时,您可以要求用户输入用户名和密码。现在最简单(但可能不是最好的)方法是将其保存在SharedPreferences中。然后,当用户下次访问应用程序时,您可以从SharedPreference进行检查,如果登录正确则继续,否则显示错误。

sendMessage功能中,您可以先检查是否有任何内容存储在sharepreference中,并使用您用于用户名和密码的特定密钥。如果没有保存,则用户第一次来。然后strat一个新的Intent,你可以保存它。如果存在一些已保存的内容,则可以从共享首选项中提取这些内容,并将值与输入的值进行比较。

现在,在SharedPreference中保存密码不是一个好主意,因为在可以访问的根设备上。您可以对密码使用散列/加密。

请参阅remember password in android app

SharedPreferences for login in android

Best option to store username and password in android app

代码片段和其他逻辑。