我想知道最常用的应用程序如何保持登录? 当我打开Facebook,Twitter,Instagram或500px我的应用程序准备就绪,没有时间登录,我需要知道我的应用程序的方法,谁请求8-10秒登录和下载用户列表。
编辑:我不是在谈论凭据的“cookie”。例如,如果您尝试使用Facebook,则可以看到第一次登录需要30秒,然后始终连接。另一次打开Facebook时,它不会要求共享首选项的凭据,但登录时间不需要30秒。为什么呢?答案 0 :(得分:1)
我相信所有上述帖子都是准确的,但我想加上我的两分钱。当您的应用程序启动时,您应该在Launcher Activity
(在主屏幕上点击您的应用程序图标时启动的活动,如果尚未打开时)启动
// This will get you an instance of your applications shared preferences.
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
// Values
String userName = preferences.getString("username",null);
String password = preferences.getString("password",null);
// Then you want to query preferences for values such as username and password like
if((userName != null) && (password != null))
{
// **** Log your user into your application auto**Magically** ********
// ------------------- Option 1 - web request -------------------
// First i Would make the initial Web Request before trying to send my User into
// a new Activity.
// Run an `AsynchTask` against your webservice on the server if this is something
// you need to do to see if the username and password, are correct and registered
//---------- Option 2 - Check the SQLite Database(if you are using one) ---------
// Otherwise you can use this info to read from an SQLiteDatabase on your device.
// To see if they are registered
// This is where you would create a new intent to start
// and Login your user, so that when your application is launched
// it will check if there are a username and password associated to the
// Application, if there is and these are not null do something like
// Create a new Intent
Intent automagicLoginIntent = new Intent(getBaseContext(),AutomagicLogin.class);
// Pass the Bundle to the New Activity, if you need to reuse them to make additional calls
Bundle args = new Bundle();
args.putString("username", userName);
args.putString("password", password);
automagicLoginIntent.putExtra("UserInfo", args);
// Launch the Activity
startActivity(automagicLoginIntent);
}else{
// Show the Layout for the current Activity
}
这应该在您的onCreate(Bundle savedInstanceState);
方法中完成,但这不足之处。实现它不应该太棘手,但实现取决于您的逻辑如何工作。
您是否从SQLIte数据库中读取? 你从WebService上读过吗?
等。
但这应该是你所需要的,除非你需要在prefs中存储一些其他的值。
旁注
如果您无法从用户那里获取此信息,即注册表单,则上述所有代码均无效。
在考虑注册时,您可以使用以下代码初始存储这些值:
// Inside an Listener , get a reference to your `TextView`s that were used to enter
// username and password
TextView usernameText = (TextView) findViewById(R.id.textViewUsername);
TextView passwordText = (TextView) findViewById(R.id.textViewPassword);
// Get a reference to the SharedPReferences, SO WE CAN BEGIN TO STORE THE VALUES ENTERED
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
// Need this to Edit Preferences
SharedPreferences.Editor editor = preferences.edit();
// Then you can add the values
editor.putString("username",usernameText.getText().toString().trim());
editor.putString("password",passwordText.getText().toString().trim());
// Must Call this to write values to the Preferences
editor.commit();
这应该是您所需要的一切,最初存储值并从首选项中读取它们,以便每次启动应用程序时自动登录您的用户如果尚未打开,如果它已经打开,则android活动生命周期应该重新加载上次暂停的当前活动。
祝你好运!
答案 1 :(得分:0)
您应该为每个用户保存身份验证令牌,每次用户打开应用程序时都不需要显式重新验证。当应用程序进行后续API调用时,请使用该身份验证令牌并将其发送到后端。如果后端告诉您身份验证无效,那么您知道需要重新进行身份验证。
答案 2 :(得分:-1)
他们使用sharedPreferences存储令牌 http://developer.android.com/reference/android/content/SharedPreferences.html