I am making one android app where one setting is must every user need to update that which is pincode.
So after successful login, if the user has not updated pincode yet, it needs to be updated mandatory before navigating to any other screen.
What is the best way to implement this? Any Ideas?
Update:
After answer, I meant to say that setting I will be fetching from firebase database as boolean flag. So my actual query is how to show that as a mandatory to get the user update? (i.e) either navigating to different activity or showing popup and getting the setting etc. I need UI design ideas.
What is the best practice?
It is not clear what is the point of this, and if you have a server side that controls that stuff, but I'll try to give a help.
If you have a Server controlling authentication:
On login, call the API of your service to check if has happened or not. You could save the answer in a "shared preference" (Android Documentation), so you don't call your API every time.
If you only want to have the application on a client side:
Use 1 to store values that indicate if the desired action was performed or not, which is verified right after the login.
EDIT:
If the action is mandatory, you could send the user to an activity to update the pin, this will happen until the action is performed.
Client side approach:
You can use SharedPreferences to persist a value, like a simple boolean, that will inform you if that the user already updated the pincode or not.
I would recommend you to perform the check in the onResume() of your Launcher Activity.
Putting it simple and explicit:
public static final String PREF_FILE_NAME = "PrefFileName";
public static final String PREF_IS_PIN_CODE_UPDATED = "PREF_IS_PIN_CODE_UPDATED";
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
Boolean isPinCodeUpdated = prefs.getBoolean(PREF_IS_PIN_CODE_UPDATED, false);
if (isPinCodeUpdated) {
// You can proceed
} else {
// Force the user to update the pin code, by prompting for instance a dialog where he can change the pin with setCancelable(false)
}
}
After you know that your user already updated the pin code you just need to set the preference to true.
SharedPreferences.Editor editor = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE).edit();
editor.putBoolean(PREF_IS_PIN_CODE_UPDATED, true);
editor.apply();
After this every time the user opens the app it will pass in the onResume() method and your isPinCodeUpdated var will be true and the app should proceed as usual.
From you question I am assuming that you didn't want a response in terms of UI but how could you have the information persisted.
Note: This implementation fails for multiple users in the same device. With few tweaks you can make it work for multiple users in the same device but in my opinion this verification should be done server side.
Related
I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}
I'd like to change the messages in the list "Reject call with message" according to the calling number (whether I have it in my contacts or if it has a certain carrier, etc) or even hide this option completely for some numbers.
What I'm asking for is a starting point since I couldn't find anything on developer.android.com nor on the internet.
Note: I don't want to reject or mute a call, no, I just want to modify the "Reject call with message" list on the fly depending on the caller or even disable it completely for some numbers.
Quick response messages can only be altered through Call settings UI as the values are stored in Phone app's shared preference, respond_via_sms_prefs.xml. See RespondViaSmsManager.java:
/** SharedPreferences file name for our persistent settings. */
private static final String SHARED_PREFERENCES_NAME = "respond_via_sms_prefs";
public void setInCallScreenInstance(InCallScreen inCallScreen) {
mInCallScreen = inCallScreen;
if (mInCallScreen != null) {
// Prefetch shared preferences to make the first canned response lookup faster
// (and to prevent StrictMode violation)
mInCallScreen.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}
Only Phone app can read/write from/to the file.
Default values are set in respond_via_sms_settings.xml.
As far as I can tell, what you are trying to achieve is not feasible at this moment.
I'd like to ask the logic for first asking the user to fill up a profile form so it is filled, and then directed to the main activity. After closing the app and then re open it again, the profile activity should not be first launched but now the main activity. How can I do this? I'm in need of help. Thanks.
This is what I have tried so far:
private void doThread(){
Thread timer = new Thread(){
public void run(){
try{
sleep(5000); // sleeps/delays for 3 seconds
} // end try
catch(InterruptedException e){
e.printStackTrace();
}finally{
// this is going to create new intent activity for
// based on the action name (com.fps.ihealthfirst.IHEALTHFIRSACTIVITY)
boolean firstTime = mPreferences.getBoolean("user_prefs", true);
if (firstTime) {
Intent myProfile = new Intent( Disclaimer.this, Profile_Pref.class );
startActivity(myProfile);
}
else{
Intent openIHealthFirst = new Intent( "com.fps.iHealthFirst.IHEALTHFIRSTACTIVITY" );
startActivity( openIHealthFirst );
finish();
}
}// end finally
} // end run method
}; // end thread
timer.start();
}
Depending on your choice, you can choose to save the information collected from the Profile Form in either a Database or a SharedPreferences file. This part is rather subjective and if you are already employing a Database in your application, you might consider it.
Here are a few suggestions on handling the logic / flow after the user has setup / entered his Profile details:
First: If you aren't already employing a Splash Screen, you must consider creating one. This will give you a small buffer time to check if the user has already entered his profile details.
Second: If he hasn't, you can open the Profile Form Activity. If, after checking either of the Database or SharedPreferences file, you find data indicating a filled form, you can display the main activity directly.
I personally, would be more inclined towards using SharedPreferences for this task.
Here are a few tutorials to get started with SharedPreferences:
http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://myandroidsolutions.blogspot.in/2012/06/android-store-persistent-data-using.html
http://moorandroid.blogspot.in/p/shared-preferences-on-android.html
They may not be specific to your question, but will give you the logic to save values. Retrieving the saved values would be fairly simple.
Hope any of this helps.
One way is to save the form information on to SD card, then load and check for the information, if the information is present there, then you can move to next activity. Check this answer for explanation to it.
Can I have an android activity run only on the first time an application is opened?
The other is to check for a specific shared preference in the main activity, if that shared preference is missing, then launch the single run activity again. Check the following answer for an explanation to it.
How to launch activity only once when app is opened for first time?
Determine if android app is the first time used
You can use SharedPreferences.I had this same question with a good answer here. Check it out.
I am new to Android App development.I have a small issue.Working on my Android Application suddenly hit "Home" Button so it goes to background after some time again go to "Home" and click my app icon it again getting started from my first screen instead of this i have keep the last viewed Activity and should show message like "Your Session timed out So Please Loging again the box with two EditText boxes" Then check the credentials and Allow the user to continue or redirect to Login Screen.How to achieve this.
Thanks in advance..
Its simple in Android. you need to maintain SharedPreferences.
Read this for better understanding of the concept.
This will work as
==> when ever you are trying to maintain login concept save user name in the sharedpreference.
==> once data is edited in prefernces.xml , the data in it can be check in any Activity.
For example, to save username, password and session ID, you can:
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putString("User Name", username);
edit.putString("Password", password);
edit.putInt("Session ID", session_id);
edit.commit();
and get them
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
username = pref.getString("User Name", "");
password = pref.getString("Password", "");
session_id = pref.getInt("Session ID", 0);
SharedPreferences is definately the way to go, but to go into more detail for the timeout:
Save the current timestamp in onPause() (-> SharedPreferences)
Then in onResume() compare it, to check for a timeout
If you did not time-out just continue, otherwise show that nice screen, simply logout, self-destruct the device, or whatever you would like to do in that case :-)
Also you might consider implementing some kind of session manager class, to handle it on a higher level:
Nice example/tutorial from android hive
See this diagram for an explanation of the Activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Override the appropriate methods in your activity to keep track of when it loses/regains focus.
If you want to maintain the user login session then instead of having the preference in all the activities we can have handler inplace to do this.
Create Handler after the user logsin
Set sendMessageAtTime(Message, long), long is millisecond value. This will call the logout function that defined.
OK, so I'm playing around with an android app.
The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen.
The first time a user loads the app however, some configuration is required before their list screen is of any value to them.
So my question, is how I can go about displaying the configuration activity the first time the app is opened up, and then the list screen for future openings.
I also want to put a demo button on the configuration screen, so I suppose more than just detecting that it's the first time, I specifically want to detect whether the user has performed certain configurations within the first screen.
After the first time the user has loaded the app you could store the details of whether user has performed the configurations in SharedPreferences.
protected void storeSharedPrefs(String value) {
/*
* Storing in Shared Preferences
*/
editor.putString("first", value);
editor.commit(); //Commiting changes
}
Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences
private boolean first_time_check() {
/*
* Checking Shared Preferences if the user had pressed
* the remember me button last time he logged in
* */
String first = uPreferences.getString("first", null);
if((first == null)){
return false;
}
else
return true;
}
i like dweebsonduty's method. a similar way to do this is to save their configuration information in files on the SD card. your app could start out by checking for those files and loading the information. if the load is successful, it moves on to the next activity, if not it starts the appropriate activity to prompt the user for input.
I have done this same thing, but instead of swiching activities i just switch views until i have all the info i need, and then move on.
Many applications actually store the current version in SharedPreferences, and check against it for if an update has been installed. Its a clever way of achieving a "what's changed" popup, or making sure that some settings get set (I would be wary of just having a boolean flag because if you ever want to add an additional setting, you will need a second flag and it gets messy after the third, etc.).
String VersionValue = "v.1.0";
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean(VersionValue, true)) {
//the app is being launched for first time, do something
NewDialogFragment newFragment = new NewDialogFragment();
newFragment.show(getFragmentManager(), "New");
// record the fact that the app has been started at least once
settings.edit().putBoolean(VersionValue, false).commit();
}
You could do it this way and still get the same result I tried it its a small workaround if u do not fully understand how to check if the app is updated. Instead with this code you can just simply change the String VersoinValue to your current app version and android will think the app is a new first time app and will only display the code u wrote once until you change VersionValue on your next update. (:
How will you be storing the configuration?
If it is in SQLlite you could just create a table called firstuse and put a field in there called hasbeenused and make it null. Then when the app is used you can put a 1 in there. Then you can read it each time your app loads and if that field = 1 then go to your next activity.