安装android应用程序后首次创建登录页面

时间:2013-03-24 13:48:45

标签: java android sqlite login android-sqlite

如果我的问题似乎重复,我道歉。但是,我仍然找不到任何可以帮助我的答案或教程。

我想开发一个在用户安装应用程序后需要登录一次的应用程序。因此,如果用户想要使用该应用程序,则用户不需要再次登录。用户的数据需要存储在sqlite中才能登录到应用程序。

是任何教程或任何可以帮助我的东西。

提前感谢。

3 个答案:

答案 0 :(得分:3)

请参阅: Android User Session Management using Shared Preferences

它包含代码示例,包含您需要的所有有用信息。并且不需要将数据存储在SQLite中,用于您在问题中描述的内容。只需使用共享偏好设置


祝你好运!

答案 1 :(得分:3)

第1步:创建Splash Activity并将其用作应用程序的入口点。 NoDisplay使其对用户不可见。它不需要布局

        <activity
        android:name="your.package.name.Splash"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoDisplay" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

第2步Activity检查用户是否已登录:

    public class Splash extends Activity {

private SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    if (!prefs.getBoolean("UserLoggedIn", false)) {

        startActivity(new Intent(this, UserLoginActivity.class));
        finish();

    } else {
        startActivity(new Intent(this, YourActivity.class));
        finish();
    }

}

       }

在用于用户登录的Activity中,创建boolean值并在登录完成后将其设置为true(示例):

            ..........
           prefs = PreferenceManager
                    .getDefaultSharedPreferences(UserLoginActivity.this);
            Editor editor = prefs.edit();
            editor.putBoolean("UserLoggedIn", true);

            editor.commit();
            startActivity(new Intent(UserLoginActivity.this,
                    NextActivity.class));

here是使用SharedPreferences

的示例

答案 2 :(得分:0)

    Ok then open a database like this 

    public SQLiteDatabase sampleDB;
        public String COLUMN_ID="_id";
        public String COLUMN1="username";
        public String COLUMN2="password";
        public String TABLE_NAME="Androdata";

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);


            sampleDB =  this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null);
            boolean x=init(TABLE_NAME);// init is a function that checks db and return 
                                                 //false only for 1st time
            if(x==false)
            {
            createDB();
            insertDB();
            }

            }
    public boolean init(String tableName)
        {
            Cursor cursor = sampleDB.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
            if(cursor!=null) {
                if(cursor.getCount()>0) {
                                    cursor.close();
                    return true;
                }
                            cursor.close();
            }
            return false;
        }
        private void insertDB() {
            sampleDB.execSQL("INSERT INTO " +
                    TABLE_NAME +
                    " Values ('0','Androviewer','viewer');");   
            System.out.println("Inserted data successfully....");
        }
        private void createDB() {
            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    TABLE_NAME+ "(" + COLUMN_ID
                    + " integer primary key autoincrement, " + COLUMN1
                    + " text not null,"+ COLUMN2
                    + " text not null);");
            System.out.println("Database created successfully....");
        }


    enter default value for id as 0 which is done here in insert function.

then all you need to do is just insert entered value in to database ok 

all the best

    check in start screen if its 0 open register page and get the values in to database otherwise go to app as your wish 
相关问题