Android 如何在应用程序启动时跳过LauncherActivity并调用另一个Activity
我有Splash Activity=>Login Activity=>Main Activity。。。。 一旦用户登录,我的用户应该直接重定向到主活动,直到注销 请给我一个具体的解决方案…在哪个活动中做什么 我正在使用web服务…建议我SQ Lite是否需要或共享首选项或会话。类 请具体说明…在哪些活动/课程中做什么 在登录之前 我想像这样登录后的流Android 如何在应用程序启动时跳过LauncherActivity并调用另一个Activity,android,sqlite,session,login,sharedpreferences,Android,Sqlite,Session,Login,Sharedpreferences,我有Splash Activity=>Login Activity=>Main Activity。。。。 一旦用户登录,我的用户应该直接重定向到主活动,直到注销 请给我一个具体的解决方案…在哪个活动中做什么 我正在使用web服务…建议我SQ Lite是否需要或共享首选项或会话。类 请具体说明…在哪些活动/课程中做什么 在登录之前 我想像这样登录后的流 Splash Activity =>Main Activity .... 提前谢谢你 SplashActivity.java 公共课堂活动
Splash Activity =>Main Activity ....
提前谢谢你
SplashActivity.java
公共课堂活动{
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();
}
}
}
}像这样
一次创建Spalsh活动
if(isLogin) //value comes from Shared Preference
{
Go to Main
}else
{
Go to Login
}
使用SharedReference,它将布尔变量存储为isUserLoggedIn。如果用户已登录,则它将存储true,否则将存储false,然后在启动屏幕上检查SharedReference的值。您可以使用它来实现此目的。您需要在活动中实现逻辑。您需要检查是否已使用存储在共享首选项中的值登录,并根据该值显示下一个活动
在启动登录活动的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();
可能重复的您可以使用SharedReference。在大数据的情况下使用SQlite,作为简短的信息使用首选项。@ravidl对于这个问题,据我所知,不需要使用SQlite。@Nigam如果他从web服务获取大数据,那么根据我所知,将大数据保留在共享首选项中不是一个好主意,这将占用大量缓存。无法工作:…我正在编辑我的问题。。将代码放入其中以查看…@AtifPatel:由于您使用的是全局共享首选项,而不是特定于应用程序的首选项,因此需要替换:String sharedPrefId=MyAppPreference;SharedPreferences=GetSharedPreferenceSharedPrefid,0;使用SharedReferences prefs=PreferenceManager.getDefaultSharedReferencesPlash.this;向您致敬先生再次感谢您节省了我这么多的工作和时间帮助……@Mithun先生。。。我想在主页上设置注销…如何从全局共享首选项注销…@AtifPatel:您可以使用prefs.edit.removiesloggedin.commit清除首选项;
prefs.edit().putBoolean("isLoggedIn", true).commit();