2016-08-11 22 views
0

私はこのメソッドを使用しました。データがデータベースにあるときに機能し、データベースが空のときには機能しません。AndroidのSQLite mainActivityの移動方法(ログインプロセス)

private boolean checkDataBase() { 


     SQLiteDatabase checkDB = null; 

     try { 
      checkDB = SQLiteDatabase.openDatabase(String.valueOf(dataBase), null, 
        SQLiteDatabase.OPEN_READONLY); 
      checkDB.close(); 
     } catch (SQLiteException e) { 

     Intent i = new Intent(getApplicationContext(), Next.class); 
      startActivity(i); 
     } 
     return checkDB != null; 


    } 

私はAndroid開発の初心者です。これは初めてのAndroidアプリケーションです。
アプリケーションに3つのActivities(スプラッシュスクリーン、ログイン、メインアクティビティ)が含まれています。

私の質問は次のとおりです。

  • databaseは値(ログイン値)が含まれている場合MAIN_ACTIVITYを起動する方法、ログインアクティビティを起動していない場合は?
  • どのような状態ですか?
  • どこで使用しますか?
+0

最初に登録アクティビティを使用し、データをデータベースに保存してください。データベースを使用してログインアクティビティを使用した後、ログインします。 – mujjuraja

+0

私は上記の方法を使用しました –

+0

最初に試してみてください。登録アクティビティを使用してデータベースにデータを保存してください。 – mujjuraja

答えて

0

SQLiteデータベースを使用してログイン値を保存しないことをお勧めします。ログイン値を保存する場合は、Shared Preferencesを使用します。共有設定hereについて読むことができます。

SplashScreen.java

public class SplashScreen extends Activity { 

private final int SPLASH_TIME_OUT = 3000; // Splash screen timer 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash_screen_layout); 

    // This method call delays the execution of the activity 
    // by 3 seconds (3000 milliseconds) 
    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // This method will be executed once the timer is over 
      Intent intent; 

      SharedPreferences sharedPrefs = getSharedPreferences("LOGINPREFS", 0); 

      /* Checks if the user has ever logged in 
      * If not, a login screen is displayed. 
      * LOGGED_IN would be the name of the shared preference key, 
      * which would have been set on the login activity, 
      * only if the login is successful 
      */ 
      if (sharedPrefs.contains("LOGGED_IN")) { 
       intent = new Intent(SplashScreen.this, MainActivity.class); 
       intent.putExtra("student", student); 
      } else { 
       intent = new Intent(SplashScreen.this, LoginActivity.class); 
      } 

      startActivity(intent); // Starts a new activity 

      finish(); // Close this activity 
     } 
     }, SPLASH_TIME_OUT); 
    } 

    @Override 
    public void onBackPressed() { 
    } // Disables the back button 
} 
+0

何をすべきか? –

+0

SQLiteには何が必要ですか? –

+0

あなたはそれを把握しましたか? –

2

あなたのデータベースを照会するために、このメソッドを使用します。

public boolean login(String username, String password) throws SQLException { Cursor mCursor = db.rawQuery( "SELECT * FROM YOUR_TABLE WHERE USERNAME=? AND PASSWORD=?", new String[] { username, password }); if (mCursor != null) { if (mCursor.getCount() > 0) { return true; } } return false; }

関連する問題