我有Splash Activity => Login Activity => Main Activity....
一旦用户登录我的用户应该直接重定向到主要活动,直到LOGOUT .....
请给我一个具体的解决方案......在哪个活动中做什么..
我正在使用网络服务....建议我,如果SQ Lite需要或共享偏好或Session.Class
.....
请具体......在哪个活动/班级中做什么......
登录前。。
Splash Activity => Login Activity => Main Activity
我希望LOGIN之后的流程就像这样..
Splash Activity =>Main Activity ....
提前谢谢.....
SplashActivity.java 公共类Splash扩展了Activity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
String sharedPrefId = "MyAppPreference";
SharedPreferences prefs = getSharedPreferences(sharedPrefId, 0);
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if(isLoggedIn)
{
// Show Main Activity
Intent intent1= new Intent(Splash.this,SnetHome.class);
startActivity(intent1);
}
else
{
// Show Login activity
Intent intent2= new Intent(Splash.this,Login.class);
startActivity(intent2);
}
//if{
//if user redirect to LoginActivity
//Intent intent = new Intent(Splash.this,Login.class);
//startActivity(intent);
//}else{
//otherwise redirect to SnetHome activity
// }
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Login.java
公共类登录扩展AppCompatActivity实现了OnClickListener {
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// php login script location:
// localhost :
// testing on your device
// put your local ip instead, on windows, run CMD > ipconfig
// or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL =
// "http://xxx.xxx.x.x:1234/webservice/login.php";
// testing on Emulator:
private static final String LOGIN_URL = "http://192.168.1.106/SnetWebservice/login.php";
// testing from a real server:
// private static final String LOGIN_URL =
// "http://www.mybringback.com/webservice/login.php";
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// setup input fields
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
// setup buttons
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);
// register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent inte = new Intent(Login.this, Register.class);
startActivity(inte);
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
/* case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
*/
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = prefs.edit();
edit.putString("username", username);
edit.commit();
prefs.edit().putBoolean("isLoggedIn", true).commit();
Intent i = new Intent(Login.this, SnetHome.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else if (success == 0) {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:0)
喜欢这个
onCreate of Spalsh Activity
if(isLogin) //value comes from Shared Preference
{
Go to Main
}else
{
Go to Login
}
答案 1 :(得分:0)
使用SharedPreference
将boolean
变量存储为isUserLoggedIn
如果用户已登录,那么它将true
存储false
,然后检查值SharedPreference
的{{1}}。
答案 2 :(得分:0)
您可以使用SharedPreference来实现此目的。您需要在SplashActivity中实现逻辑。您需要使用共享首选项中存储的值检查是否已登录,并根据该值显示下一个活动。
在SplashActivity
(启动登录活动的位置)中,添加如下逻辑:
// Retrieving your app specific preference
String sharedPrefId = "MyAppPreference";
SharedPreferences prefs = getSharedPreferences(sharedPrefId, 0);
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if(isLoggedIn)
{
// Show Main Activity
}
else
{
// Show Login activity
}
在您的LoginActivity
中,成功登录后将值设置为true:
prefs.edit().putBoolean("isLoggedIn", true).commit();