on the weekend I started to build my first android app. As I need to ask the user of my app for user credentials [which are used for further webservice usage] I want to simulate a "login system". On the start of my app the user should be told to enter his credentials. When the user is inactive for too long I want to dismiss the entered credentials and to "log out" the user.
While coding and afterwards while testing I realized that the way I thought I could go doesn't work. After reading the docu and several SO-questions again and again I question myself more and more if I have understand the app / activity life cycle and its possibilites fully. So I wanted to ask for help in understand the life cycle and its linked influences on my app. So yes this might be several questions in one :/
For the moment my app consists of the following activities:
a search activity (which is opened once the app is started)
a settings acitivy (which can be accessed from the search dialog and has a link back to the search dialog)
After the user has entered an ID in the search dialog I want to open an activity regarding to the search result (NYI).
When starting to implement the user auth, my idea was the following:
Everytime onResume() of an activity is called I need to check a) if user credentials are already stored and b) if the last action of the user is less then X minutes ago. If one these fails I want to show a "log in panel" where the user can enter his credentials, which are then stored in the SharedPreferences. For that I did the following:
I first build an parent activity which has the check and a reference for the SharedPreferences in it
public class AppFragmentActivity extends FragmentActivity {
protected SharedPreferences sharedPref;
protected SharedPreferences.Editor editor;
protected String WebServiceUsername;
protected String WebServicePassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appfragmentactivity);
}
#Override
protected void onResume () {
super.onResume();
// Check if user is "logged in".
// Meaning: Are there given user credentials and are they valid of was the user inactive for too long?
// We only do this "onResume" because this callback is the only one, which is called everytime an user
// starts/restarts/resumes an application
checkForUserCredentials();
// Set new "last action" now "now"
setLastAction(new Date().getTime());
}
#Override
protected void onStart () {
// Fill content
super.onStart();
// Set global sharedPreferences
sharedPref = this.getSharedPreferences(getString(R.string.FILE_settings_file), Context.MODE_PRIVATE);
}
/*
* Checks if user credentials are valid meaning if they are set and not too old
*/
private void checkForUserCredentials() {
long TimeLastAction = sharedPref.getLong(getString(R.string.SETTINGS_USER_LAST_ACTION), 0);
long TimeNow = new Date().getTime();
// Ask for User credentials when last action is too long ago
if(TimeLastAction < (TimeNow - 1800)) {
// Inactive for too long
// Set back credentials
setUsernameAndPassword("", "");
}
else
{
WebServiceUsername = sharedPref.getString(getString(R.string.SETTINGS_USER_USERNAME), "");
WebServicePassword = sharedPref.getString(getString(R.string.SETTINGS_USER_PASSWORD), "");
}
}
/*
* Saves the given last action in the sharedPreferences
* #param long LastAction - Time of the last action
*/
private void setLastAction(long LastAction) {
editor = sharedPref.edit();
editor.putLong(getString(R.string.SETTINGS_USER_LAST_ACTION), LastAction);
editor.commit();
}
/*
* Saves the given username and userpassword sharedPreferences
* #param String username
* #param String password
*/
private void setUsernameAndPassword(String username, String password) {
editor = sharedPref.edit();
editor.putString(getString(R.string.SETTINGS_USER_USERNAME), username);
editor.putString(getString(R.string.SETTINGS_USER_PASSWORD), password);
editor.commit();
WebServiceUsername = username;
WebServicePassword = password;
}
/*
* Method called when pressing the OK-Button
*/
public void ClickBtnOK(View view) {
// Save User-Creentials
EditText dfsUsername = (EditText) findViewById(R.id.dfsUsername);
String lvsUsername = dfsUsername.getText().toString();
EditText dfsPassword = (EditText) findViewById(R.id.dfsPassword);
String lvsPassword = dfsPassword.getText().toString();
if(lvsUsername.equals("") || lvsPassword.equals("")) {
TextView txtError = (TextView) findViewById(R.id.txtError);
txtError.setText(getString(R.string.ERR_Name_or_Password_empty));
}
else
{
// Save credentials
setUsernameAndPassword(lvsUsername, lvsPassword);
setLastAction(new Date().getTime());
// open Searchactivity
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
}
}
The "log in mask" is setContentView(R.layout.activity_appfragmentactivity);.
The two other activites I created are then extending this parent class. This is one of it:
public class SearchActivity extends AppFragmentActivity {
SearchFragment searchfragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
#Override
protected void onResume() {
super.onResume();
if(WebServiceUsername.equals("") && WebServicePassword.equals("")) {
// Username not set. Re"login".
Intent intent = new Intent(this, AppFragmentActivity.class);
startActivity(intent);
}
}
#Override
protected void onStart() {
super.onStart();
}
// ...
}
As far as I understand the lifecycle now this should work as the following: When my app starts (the SearchActivity is set for LAUNCH) the app should step into the onResume() of my parent class. There it sees that the credentials are not yet stored and opens the layout of the AppFragmentActivity which is the login. When entered, the user is redirected to the SearchActivity which now sees "ok credentials are there, lets move forward". But this doesnt't happen as the login is not shown up. So I think my onResume() might be wrong. Perhaps my full idea is bad? Up to here I thought I also understand the life cycle, but obviosly I don't?
I then had a look around on SO for similar problems. One thing I saw here was a comment to an user which wanted to build a similar "logout" mechanism as mine, that he has to implement this in every activity. I thought about that and ask myself "Why do I have to override the onResume() in every of my activites, when they are all from the same parent? When theres no onResume() in the child, the one of the parent should be called". The user in the SO-question was advised to use services as background threads to count down a timer in there for the logout. I then read the services article in the docu and then fully got disoriented:
There are two types of services: Started and bounded ones. A started service is once started by an activity and then runs in the background until hell freezes when it doesn't get stoped. So it's fully independed of any app, but the programmer has to / should stop it when it's not longer needed. A bounded services is bounded to one or many app components and stops when all bounded components end. I thought this might be a good alternative for me, but when I thought further I ask myself how: If one of my starts it (let's say the login dialog) and then is closed the service is stoped and the other activites always start there own ones which can't be the sense of it. So this service must be bounded not to any component but to my app. But whats the life cycle of an android app? How can I keep information "global" inside my app. I know I can switch data between actitivites using 'Intents'.
This more and more "foggy cloud" lead to ask myself: "Shall I use only one activity and try to switch in/out everything using fragments?"
So my questions are (I think that's all of them, but I'm not sure anymore):
Does my idea of writing an parent class which does the checks for all extended childs ok or bad AND does it work as I understood it?
Do I have to override every onResume() in the childs just to call the parent one for the checks?
Can you give me a tip why my "login systems" doesn't work?
What's the life cycle of an android app and how can I interact with it?
Shall I only use one activity and switch in/out everything using fragments or is it a good way to have several activities and some of them use fragments (to reuse often used parts)?
Thanks in advise
What I've done in the end is the following:
I removed the "login" thing from the parent class into a stand alone activity. This activity is called when the credentials are not valid together with an finish() of the calling one. So I don't build a loop and drop unused activites.
Related
What I'm trying to do
I am trying to use Android's Switch element to login or logout users from Facebook and LinkedIn's APIs, as shown in the image.
My issue
Currently the toggles trigger automtically on user click. I'd liek them to only move on command when I the API has confirmed that the user's login state has successfully changed.
My attempt
I have tried to use myLoginSwitch.setClickable(false); however this doesn't seem to have any affect.
What I'm trying to avoid
I imagine this could be done with a custom made switch element, however for obvious reasons I am trying to avoid that solution!
Any ideas would be much appreciated!
Thanks for all the thoughts & answers!
Disabling the button wasn't something I was keen on and sadly using onClick() isn't enough to overwrite the default action.
However onTouch did the trick perfectly! If anyone is doing something similar, this is the function:
myLoginSwitch.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
myLoginSwitch.setClickable(false);
myLoginLogoutFunc();
return false;
}
});
Try something like this. Notice I used pseudocode to handle the facebook login. But Facebook or any other organization is going to provide methods for you to deal with login success/failure in their API. Use the method to set the Switch to "checked" if logged in successfully or set checked to false otherwise:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.widget.Switch facebookSwitch = (android.widget.Switch)findViewById(R.id.mySwitch);
//This pseudocode programs what to do after a login attempt.
//Real API's have code that looks extremely similar to this.
final FacebookHandler fbHandler = new FacebookHandler(new ILogin() {
#Override //method called after calling fbHandler.login();
public void loginAttemptResponse(boolean wasSuccessful) {
//is login was successful, check the Switch, otherwise, uncheck it.
facebookSwitch.setChecked(wasSuccessful);
//re-enable the Switch since login/logout activity is finished.
facebookSwitch.setEnabled(true);
}
});
//This code block handles what happens when you press the Switch.
facebookSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//disable the switch when attempting to login/logout
facebookSwitch.setEnabled(false);
//if the Switch isn't checked, log in.
if(!facebookSwitch.isChecked())
fbHandler.login();
else
fbHandler.logout(); //User already logged in, so log out.
}
});
}
}
I'm new to android, but in the interest of practicing and learning more I wanted to try to make a practice application that essentially allows the user to only be logged on within the minute that they first logged in. So in other words if you log in at 10:23, the user will be logged out as soon as the clock strikes 10:24. To do this, I log in via my LoginActivity I share the current minute in a SharedPreferences object. In the interest of making use of the onResume() method in my AppActivity, my application first opens the main activity,AppActivity, which then opens my LoginActivity in the onCreate() method of my AppActivity class. Hopefully without being too redundant in my explanation, the goal is to call sessionChecker_AsyncTask.execute() in the onResume() method of my AppActivity.
Here's my login button found in LoginActivity:
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int result = getTime();
SharedPreferences pref = getSharedPreferences(PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt(SESSION, result);
editor.commit();
finish();
}
});
with helper method getTime() seen here:
private int getTime() {
Calendar time = Calendar.getInstance();
return time.get(Calendar.MINUTE);
}
As you can see I'm simply storing the current minute in a SharedPreferences object via my login button. Now from here, I wanted to create an AsyncTask in my AppActivity class that checks every 500ms (.5 seconds) if my current minute in AppActivity matches the minute that was stored in my SharedPreferences object, if not I want to essentially log the user out and send them back to the LoginActivity.
My issue is, I'm a bit confused about the types that I should be implementing in my AsyncTask for my doInBackground() and onPostExecute() methods. Also do I need the onCancelled() method, and would implementing it perhaps be useful for configuration changes within AppActivity? As of now I'm thinking that I should set it up something like this:
private class SessionCheckerTask extends AsyncTask<Integer, Void, Boolean>
{
#Override
protected Boolean doInBackground(Integer... minute_in_SharedPreferences) {
//Create a thread that refreshes every 500ms that checks current time
return false; //if the current time doesn't match the time in shared preferences
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
/*
If false was returned create a toast that says "Activity session has expired"
then open the login activity again
*/
}
}
But then I'm also thinking that it shouldn't be necessary for doInBackground() to have a type at all. Instead onPostExecute() should only be called if the minute passed in doInBackground() doesn't match the current minute, but I'm not sure how to implement this in the typical AsyncTask format.
try this:
In your doInBackground:
do {
if (getTime >= pref.getInt (SESSION, result)) break;
try{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
In your onPostExecute:
Toast.makeText(context, "Activity session has expired", Toast.LENGTH_SHORT).show();
startActivity(new Intent(context, LoginActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
P.S.: I think it's wrong way to remember a minute and then compare current minute with your saved one. In case when it was, for example, 21:59 you save int "21", but in next second it will be "22". Try to do the same, but with seconds. Then in doInBackground make float a = pref.getInt (SESSION, result), in do..while cycle make a+=0.5
I have three java files in my Android project. Two are activities (MainActivity and GeoActivity) and one is a plain java file (PostHttp -> sends data to server via the HTTP POST)
I switch over to GeoActivity via a simple button on-click method. GeoActivity returns the co-ordinates of the current location in a TextView AND sends them to a remote server via the HTTP POST.
I have a Handler.class which executes sends the Post Message after a delay of 50s. Something like this below. The problem i have is that when i click the back button and switch over to MainActivity i can still see in LogCat the echoes receiving from the server that the data is still being sent. How can i stop that?
GeoActivity.class
public class GeoActivity extends Activity {
Location location;
private Handler mHandler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
....
if(location != null){
mHandler.postDelayed(updateTask,0);
}
...
}
...
public Runnable updateTask = new Runnable(){
public void run(){
mlocListener.onLocationChanged(location);
//send coordinates with a delay of 50s
new PostHttp(getUDID(),latitude,longitude).execute();
mHandler.postDelayed(updateTask, 50000);
}
Try acting on the activity's life cycle.
For example:
#Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
// Save the note's current draft, because the activity is stopping
// and we want to be sure the current note progress isn't lost.
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());
values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());
getContentResolver().update(
mUri, // The URI for the note to update.
values, // The map of column names and new values to apply to them.
null, // No SELECT criteria are used.
null // No WHERE columns are used.
);
}
This doesn't destroy the activity, it will reside in memory. However, you can always resume when needed.
Source:
Stopping and Restarting Android Activities
I have an onCreate method that runs the code below. In a nutshell the code retrieves data from the server and shows it on the screen for a messaging program. It only does it once, but I would like it to run the AsyncTask every 3 seconds (to try to simulate a chat). I'm pretty sure this is not the way to go about having a chat system but, I just need something that works for now (as a proof of concept) and I'll focus on the correct way of implementing it later.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_box);// sd
final Functions function = new Functions();
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
whatroom = prefs.getString("chat", "null");
new AsyncTask<String, Void, String>() {
#Override
protected String doInBackground(String... args) {
return function.getInbox(args[0]);
}
#Override
protected void onPostExecute(String result) {
TextView inbox = (TextView) findViewById(R.id.inbox);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setVisibility(View.GONE);
inbox.setText(result);
}
}.execute(whatroom);
}
I've tried putting a simple while statement around the asynctask but, it just force closes.
You cannot reuse an AsyncTask instance. You would need to create fresh instances each pass of your loop.
Without additional information, it's difficult to give you a specific answer. However look into abstracting everything using a Loader, using a Service, etc
Regarding Loaders:
They are available to every Activity and Fragment.
They provide asynchronous loading of data.
They monitor the source of their data and deliver new results when the content changes.
They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.
when testing an activity in my app I see that it's not using the values I've set using the PreferenceActivity.
I can confirm that the values are corretly set in the PrefsActivity (at least "locally"), because every time I open it, the settings are exactly like they were when I closed it the last time...
Do I have to specify in my PreferenceActivity which preference file to store the settings into, or is it a problem with the methods I'm using to import those values for use in my activity?
It feels like I've searched all over the web without ever finding a clear answer to that question...
This is where my activity is supposed to load the preferences, does it look right?
I know that's the only thing missing, because the calculation works just fine when I run it in debug mode and manually input the values to use...
public void OnStart() {
// Loads the values for the calculator to use
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
bodymass = settings.getInt(getString(R.string.pref_key_user_mass).toString(), 0);
gender = settings.getString(getString(R.string.pref_key_user_gender).toString(), "");
Also, does this following code look right to you?
(I would also be grateful if someone told me how to make an 'if' statement comparing several variables at once - e.g. if (one out of three fields are empty) {do something})
//Checks defined user gender and sets value to be used by calc accordingly
if (gender == "male") {
genderValue = 0.7;
}
else if (gender == "female") {
genderValue = 0.6;
}
else if (gender == "") {
settingsAlert();
}
It never seems to trigger the settingsAlert()-function, even when all app data is wiped (it should then spawn an alert message, prompting the user to go set the preferences before using, but nothing happens)
Here's the code that's supposed to spawn the alert:
public void settingsAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("#string/dialog_setPrefs_text")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent gotoSettings = new Intent(ActivityCalc.this, ActivitySettings.class);
startActivity(gotoSettings);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.create();
}
--UPDATE--
I have now managed to get the alert dialog spawn like it should, I figured I'd post the code lines that made it happen, so others with the same problem can watch and learn... :)
The problem appeared to be that the alert was indeed created correctly, but never actually called to display - therefore everything worked perfectly once I added that little .show() after the .create() in the last line.
Alternatively, you can define it as an AlertDialog object, and just call it whenever you feel like it:
AlertDialog alert = builder.create();
alert.show();
I'm posting the contents of my PreferenceActivity class here for you to see
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This line will import the preferences to display in the settings activity
addPreferencesFromResource(R.xml.settings);
}
To see how to build the preference resource file, look at this article
I've no longer included the string for specifying the name of the shared prefs to use, as I changed the get-method to use DefaultSharedPreferences instead:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
I have now only defined and initialized the variables required to be persistent for the rest of the activity:
private double genderValue = 0;
private String gender = "";
private int bodymass = 0;
The app now works exactly like it should - not crashing, and correctly loading the preferences as set in the PreferenceActivity.
You need to use equals for the IF-ELSE sentences
if (gender.equals("male")) { //equals
genderValue = 0.7;
}
else if (gender.equalsIgnoreCase("female")) { //Here the equalsIgnorecase one
genderValue = 0.6;
}
else if (gender == "") {
settingsAlert();
} else {
settingsAlert(); //Probably it always comes here.
}
Another thing.. did you saved the preferences correctly with 'commit' ?
settings.edit().putString(getString(R.string.pref_key_user_gender).toString(), gender).commit();
--After 3rd comment---
Never tried. One for each works fine. But if you check 'duanhong169' comment, there're 2 following putString and the last commited, so.. make a try :)
Anyway you can just do:
SharedPreferences.Editor editor = settings.edit();
editor.putString("gender", genderValue);
editor.putInt("bodymass", bodymassValue);
editor.commit();
And answering your other question.. you need the 'edit' to save a prefference, is how it works. you can use it at onpause, onrestart, or just after you've the data you need (inside a method).
Ie if you need a login data, once after you checked the login is correct, and if you've a different login-pass value stored (different user), you can edit.putString to save the new value inside onResult confirmation.
The better thing you can do is try different methods of saving to prefferences with different tags and then creare a Log and do getString() from each tag, so you'll know which one works, and then you can choose the better/easier for your case.
-- UPDATE 2 --
As far as I know.. (i've never tried that, and like you, i'm really new in android) your preferences.xml should be placed at res/xml and then:
public class PreferencesFromXml extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
and to get the data:
PreferenceManager.setDefaultValues(this, R.xml.prefer, false);
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
p.getString("key", "value_default")