2016-11-15 2 views
0

私は自分のアプリケーションをオープニングページでfacebookのように見せたいと思っています。ユーザーがログインページにログインしてもスプラッシュページからホームページに直接移動することはありません。私のアプリには何が起こっているのは、ログインページがまだ表示されているが、sharedpreferenceが読み込まれているので、共有に保存されている詳細をログインする必要があるということです。私は何をしたいのですが、ログインページはもう表示されません。より良い理解のための私のコードはここにあります。 :)スプラッシュからメインページへのsharedpreferenceの作成方法

スプラッシュ

public class Splash extends AppCompatActivity { 
SharedPreferences pref; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 
    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE); 

    Thread myThread = new Thread(){ 
     @Override 
     public void run() { 
      try { 
       sleep(2000); 
       Intent in = new Intent (getApplicationContext(),LoginActivity.class); 
       startActivity(in); 
       finish(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    myThread.start(); 
} 
} 

Loginpage

public class LoginActivity extends AppCompatActivity { 

final String TAG = this.getClass().getName(); 

Button btnLogin; 
EditText etUsername, etPassword; 
TextView tvRegister; 
SharedPreferences pref; 
SharedPreferences.Editor editor; 
HashMap<String, String> postData = new HashMap<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    etUsername = (EditText) findViewById(R.id.etFirstname); 
    etPassword = (EditText) findViewById(R.id.etPassword); 
    btnLogin = (Button) findViewById(R.id.btnLogin); 
    tvRegister = (TextView) findViewById(R.id.tvRegister); 

    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE); 
    editor = pref.edit(); 
    editor.putBoolean("hasLoggedIn", true); 
    editor.commit(); 

    boolean hasLoggedIn = pref.getBoolean("hasLoggedIn", false); 

    if(hasLoggedIn){ 
     String username = pref.getString("username", ""); 
     String password = pref.getString("password", ""); 

     if (!username.equals("") && (!password.equals(""))) { 
      postData.put("username", username); 
      postData.put("password", password); 
      authenticate(postData); 
     } 

     btnLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (TextUtils.isEmpty(etUsername.getText().toString())) { 
        Toast.makeText(LoginActivity.this, "Username is empty", Toast.LENGTH_SHORT).show(); 
        return; 
       } 
       if (TextUtils.isEmpty(etPassword.getText().toString())) { 
        Toast.makeText(LoginActivity.this, "Password is empty", Toast.LENGTH_SHORT).show(); 
        return; 
       } 
       editor = pref.edit(); 
       postData.put("username", etUsername.getText().toString()); 
       postData.put("password", MD5.encrypt(etPassword.getText().toString())); 
       editor.commit(); 
       authenticate(postData); 
      } 
     }); 
    } 


    tvRegister.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent in = new Intent(LoginActivity.this, RegisterActivity.class); 
      startActivity(in); 
     } 
    }); 


} 

private void authenticate(final HashMap<String, String> postData) { 
    PostResponseAsyncTask task1 = new PostResponseAsyncTask(LoginActivity.this, postData, 
      new AsyncResponse() { 
       @Override 
       public void processFinish(String s) { 
        Log.d(TAG, s); 
        if (s.contains("renter")) { 
         // Login success, Save to prefs 
         editor = pref.edit(); 
         editor.clear(); 
         editor.putString("username", postData.get("username")); 
         editor.putString("password", postData.get("password")); 
         editor.putString("userlevel", s); 
         editor.commit(); 

         Toast.makeText(LoginActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show(); 
         Intent in = new Intent(LoginActivity.this, RenterTabs.class); 
         startActivity(in); 
         finish(); 

        } else if (s.contains("owner")) { 
         // Login success, Save to prefs 
         editor = pref.edit(); 
         editor.clear(); 
         editor.putString("username", postData.get("username")); 
         editor.putString("password", postData.get("password")); 
         editor.putString("userlevel", s); 
         editor.commit(); 

         Toast.makeText(LoginActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show(); 
         Intent in = new Intent(LoginActivity.this, OwnerTabs.class); 
         startActivity(in); 
         finish(); 
        } else if (s.contains("driver")) { 
         editor = pref.edit(); 
         editor.clear(); 
         editor.putString("username", postData.get("username")); 
         editor.putString("password", postData.get("password")); 
         editor.putString("userlevel", s); 
         editor.commit(); 

         Toast.makeText(LoginActivity.this, "Driver Login Successful!", Toast.LENGTH_SHORT).show(); 
         Intent in = new Intent(LoginActivity.this, DriverTabs.class); 
         startActivity(in); 
         finish(); 
        } else if (s.contains("-1")) { 
         Toast.makeText(LoginActivity.this, "Wrong username or password...", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
    task1.execute("http://carkila.esy.es/carkila/authenticate.php"); 
} 
} 

ところで、私は、ログイン用のユーザレベルを使用しています。 Thaanks :)

答えて

0

このよう

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE); 
    editor = pref.edit(); 
    editor.putBoolean("hasLoggedIn", true); 
    editor.commit(); 

    boolean hasLoggedIn = pref.getBoolean("hasLoggedIn", false); 

    if(hasLoggedIn){ 
     // that means no need to show your login page. so go to main activity 
     Intent in = new Intent (getApplicationContext(),MainActivity.class); 
     startActivity(in); 

    }else{ 
     setContentView(R.layout.activity_main); 

      ................. 

      // others code goes here for login activity.. 
    } 

} 
を試してみてください、あなたのスプラッシュスクリーン...内部のスレッドで同じログインを適用することができ

...

// check user login status from sharedpreference 
if(loggedIn){ 
    // go to main activity 
}else{ 
    // go to login activity 
} 
関連する問題