Android: SharedPreferences default value of a String - android

I searched through a lot of questions but i didn't find one that answers my problem. So here I go :I made a custom PIN locked Activity for my App that runs as the LAUNCHER activity. This is the code I used in the onCreate() method to check if this is the first run of the app (in order to run the appropriate code and give the user the chance to set up their PIN)
if (sharedPinDatabase.getString("pin", "NO PIN SAVED").equals("")) {
Log.i("SECURITY", "NO PIN SAVED");
tvInstructionsPin.setText(" It appers that you havent saved a PIN. Enter new PIN below.");
}else{}
But whenever I run the App for the first time (or clean all the app data) the App skips this chunk of code and goes straight to the "enter your saved PIN" part(Picture 1).I used Log.i to check what value it is given to the key ("pin") before i enter any PIN with this
Log.i("SECURITY", "PINs dont match Saved one is: " + sharedPinDatabase.getString("pin", "") + "entered is : " + etPinInput.getText().toString());
and what i get from this chunk of code is "PINs dont match Saved one is: entered is :"
And if i press "OK" without typing in any number (leaving the EditText blank)it works and goes to the next Activity.
Also in the settings part of the app i made this (to run at onClick)
SharedPreferences sharedPrefWrite = getContext().getSharedPreferences("PIN_PREFS_DATABASE", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefWrite.edit();
editor.putString("pin", "").commit();
Intent i = new Intent(getActivity(), LockScreen.class);
startActivity(i);
And for some reason it works(when setting the value of the string to "") and gets me to the desired screen (the one that i made for the first run of the App)
this one (Picture2):

When you put sharedPinDatabase.getString("pin", "NO PIN SAVED").equals("") - if there was no PIN saved before, it will not return "", but it will return "NO PIN SAVED" (as this is default data you provided).
Just change it to sharedPinDatabase.getString("pin", "").equals("") - and it would work.
Small tip (for future).
If you use ""..equals(sharedPinDatabase.getString("pin", "")) instead of sharedPinDatabase.getString("pin", "").equals("") - you will avoid NullPointerException in case when sharedPinDatabase is null.

Related

Receive barcode scanner Device result in android

I am trying to receive the scanned barcode result from a device paired via (Bluetooth/USB) to an android device.
so many topics said :
most plug-in barcode scanners (that I've seen) are made as HID profile devices so whatever they are plugged into should see them as a Keyboard basically.
source
So I am using this code to receive the result of the scan:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (viewModel.onTriggerScan()) {
//1
char pressedKey = (char) event.getUnicodeChar();
viewModel.addCharToCode(pressedKey);
//2
String fullCode = event.getCharacters();
viewModel.fullCode(fullCode);
//check if the scan is done, received all the chars
if (event.getAction() == EditorInfo.IME_ACTION_DONE) {
//does this work ?
viewModel.gotAllChars();
//3
String fullCode2 = event.getCharacters();
viewModel.fullCode(fullCode2);
}
return true;
} else
return super.dispatchKeyEvent(event);
}
Note: I don't have a barcode scanner device for the test.
which code will receive the result ?? (1 or 2 or 3 ?)
You won't ever see an IME_ACTION_DONE, that's something that's Android only and an external keyboard would never generate.
After that, it's really up to how the scanner works. You may get a full key up and key down for each character. You may not, and may receive multiple characters per event. You may see it finish with a terminator (like \n) you may not- depends on how the scanner is configured. Unless you can configure it yourself or tell the user how to configure it, you need to be prepared for either (which means treating the data as done either after seeing the terminator, or after a second or two once new data stops coming in.
Really you need to buy a configurable scanner model and try it in multiple modes and make ever mode works. Expect it to take a few days in your schedule.
Workaround solution but it works 100%.
the solution is based on clone edittext (hidden from the UI), this edit text just receives the result on it, adds a listener, and when the result arrives gets it and clears the edittext field. An important step, when you try to receive the result(trigger scan) make sure that edittext has the focus otherwise you wil not get the result.
Quick steps:
1- create editText (any text field that receives inputs) in your layout
2- set its visibility to "gone" and clear it.
3- add onValueChangeListener to your edittext.
4- focus your edittext when you start trigger the scan
5- each time you the listener call, get the result and clear edittext
Note: never miss to focus your edittext whenever you start trigger scan.
Note: this method work(99%) for all external scan device and any barcode type.

How to measure the cold/warm startup time of a mobile application

Is there an easy possibility to measure the startup time an application takes? Similar questions are asking regarding self-made apps, but in this case I am talking about foreign apps to test and compare them. So far I couldn't find a simple app in the Play Store/iTunes App Store which just measures the time from the point where the user clicks the app icon to the state where it is fully loaded and can be worked with. I'd be thankful for any hints!
Put this code as your first line of code in the main activity :
Log.d("MyApp start time", "" + System.currentTimeMillis());
And after you consider the app loading ended put:
Log.d("MyApp end time", "" + System.currentTimeMillis());
Now just search the log for the info and you will the times over there
Try this tutorial or create separate application which will start your app (via Intent) and write to log (or save to file) current time (System.currentTimeMillis()) just before startActivity(intent).
Update:
In case foreign apps of You have create separate application which will start other app (via Intent) and write to log (or save to file or just store in variable) current time System.currentTimeMillis() just before startActivity(intent) like that:
Intent intent = getPackageManager().getLaunchIntentForPackage("<package of other app>");
if (intent!= null) {
Log.d(TAG, "Starting time: " + System.currentTimeMillis());
startActivity(intent);
}
(You can get other app package and main activity name this way) and than get launched apps list (or monitor logcat of them) like in that thread and when app appears in list or its log detected You can store timestamp of launched app. Difference between stored values indicates time for launch.

how to make a screen in android that runs only when the app installs

I am making an Android project. In that project i want a screen to appear only during the app installation and not every time we start the app. For eg. in whatsapp the page in which we put our name appears only during installation. I want exactly that type of a screen. I am fairly new to android programming so any help will be appreciated. Thanks! Cheers!
You can use the SharedPreferences to identify if it is the "First time" the app is launched.
Just use a Boolean variable ("app_first_time") and change its value to false after your task runs for the first time.
final String PREFS_NAME = "PrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("app_first_time", true)) {
//here the app is being launched for first time, launch your screen
settings.edit().putBoolean("app_first_time", false).commit();
}

How to sign in once in an android application?

i'm making an app in android, it has more than one option in the menu, and the login information is needed to proceed in every option, so i want to make the user sign only once not each time he's choosing one of the options like the facebook application i have to sign in only once even when the application is running in the background it doesn't logout..
sry 4 the long explanation but i don't know even the keyword for searching about this issue..
can u please help me guys ?
Thank u
Once the user enter the login credential first time then use SharedPreferences and set the value to isLogged to 1.
Next time when user open the app then check this variable if this variable value is '1' then open the home activity.
SharedPreferences sharedPref = getSharedPreferences("data",MODE_PRIVATE);
int number = sharedPref.getInt("isLogged", 0);
if(number == 0) {
//Open the login activity and set this so that next it value is 1 then this conditin will be false.
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("isLogged",1);
prefEditor.commit();
} else {
//Open this Home activity
}

Implementing a login screen with tabs

I am working on an application which requires me to create a log in screen. The way I have planned to do this is have 2 tabs log in, sign up and what I wanted to know is I want to be able to have a user sign up and if they choose remember password next time they load the app it should go straight to the main menu. Although selecting sign out in the main menu should load up the tabs with the log in information when they start the app again.
The question I have is how do I implement the remember me button so next time it skips the log in and how do I implement the sign out so next time the app loads the log in screen.
Thank you in advance! (",)
Sri
Throw the remembered answer into the SharedPreferences and read it when your activity starts and process it accordingly.
I would probably use Internal Storage to store the username/password for the remember me button.
When the app loads, first check if the user/pass is already saved. If so then direct to the tabs, if not then direct to the log in screen.
First Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(context);
Editor ePrefrences = sSession.edit();
ePrefrences.putString("id", "user id");
ePrefrences.putString("password", "user password");
ePrefrences.putBoolean("successfullylogin", true);
ePrefrences.commit();
Second Log In :
SharedPreferences sSession = PreferenceManager.getDefaultSharedPreferences(this);
if (sSession .getBoolean("successfullylogin", false)) {
//get user name and password
sUser = sSession.getString("id", "");
sPassword = sSession.getString("password", "");
//start activity
}
else {
//prepare for normal login
}

Categories

Resources