0

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

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

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

提前致谢。

4

3 回答 3

4

第 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));

是一个使用示例SharedPreferences

于 2013-03-24T14:16:19.803 回答
3

请参阅: 使用共享首选项的 Android 用户会话管理

它有代码示例,并包含您需要的所有有用信息。对于您在问题中描述的内容,无需将数据存储在 SQLite 中。只需使用Shared Preferences


祝你好运!

于 2013-03-24T14:06:01.820 回答
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 
于 2013-03-24T14:19:53.627 回答