The best way to store your Token
is when the user is successfully connected, that is, when the Token
has already been successfully created. After that, just save the Token
.
For example, the user connected to LoginActivity
and was redirected to the HomeActivity
, then...
Home Activity
public class HomeActivity extends Activity {
SharedPreferences mPref = null;
@Override
public void onCreate (Bundle cicle) {
super.onCreate(cicle);
setContentView(R.layout.activity_home.xml); // Layout
// SharedPreferences
mPref = getSharedPreferences("tokenId", MODE_PRIVATE);
}
@Override
public void onResume () {
super.onResume();
mPref.edit().putInt("tokenId", "9102887").apply();
}
}
Login Activity
public class LoginActivity extends Activity {
SharedPreferences mPref = null;
@Override
public void onCreate (Bundle cicle) {
super.onCreate(cicle);
setContentView(R.layout.activity_home.xml); // Layout
// SharedPreferences
mPref = getSharedPreferences("tokenId", MODE_PRIVATE);
}
@Override
public void onResume () {
super.onResume();
// Se o token não for encontrado, o valor será 0
int token = mPref.getInt("tokenId", 0);
// Envia um pedido de token pro servidor
// recebe o token e compara com o token salvo
// se forem semelhantes, entra na conta direto
int tokenServer = webS.getToken(deviceId, user, pass);
if(token == tokenServer) // sucesso
}
}
You don’t need necessarily save the Token
as Int
. It was just an example, I believe the recommended for your case is a String
same. And it doesn’t change much, just putInt
for putString
. See on documentation.
Source: Sharedpreferences
Do you doubt that you do not know how to use Sharedpreferences to write the Token? Or do you know how to use it but the problem is to use it in this context?
– ramaral
I know how to use, I saw some examples recording user and pass and I was able to play, but I couldn’t fit into the context of Token, and I couldn’t find many examples
– Jonas Vieira