Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016

Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016 - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article –, article (LATEST, article 2016, article android, article Login, article MD5, article Me, article MySQL), article PHP, article Remember, article SharedPreferences, article Studio, article Tutorial, we can write this you understand. Well, happy reading.

Title : Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016
link : Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016

Baca juga


Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016

Hello everyone, my name is Oum Saokosal.

Today I'm gonna teach about Android SharedPreferences.

I will use it for remember username and passwordin the log-in page.

So, first you have to know how to read datafrom PHP MySQL.

For that I use one of my libraries, I call"generic_asynctask_v2" and then you use shared preferences to store data and lastly we haveto encrypt data to MD5.

So I use a library of mine as well.

I call it "MD5 Simply".

So all of these in github.

Com/kosalgeek.

So, this is my youtube channel.

Please subscribe it.

If you want to watch another video just goand browse it here.

There are two languages English and Khmerwhich is my native language.

This is the github.

You go to repositories and you will find generic_asynctaskhere and MD5 Simply and you go here just click download and grab the GenAsync.

1.

2.

Tarand MD5 Simply Also download and grab MD5 Simply.

Because I downloaded it already so I willgrab the code so Let's add the library first and zip it, open it and copy, you go to androidhere.

Let's create a project, use the blank one.

So to add a library you click over here andchoose project and you will find add and you go to libs and right-click here paste, andthen right-click again on the jar file and you say Add AsLibrary here, OK.

So you need another library.

This one just for MD5 so to connect to thewebsite you need another library called generic_asynctask_v2 so copy again and right-click.

Paste it here,OK.

Right-click again and choose Add AsLibrary.

So you can go back to android, you have tomake sure that the library is already added, go to gradle & the second one & you will seethese two lines, it means you are good to go.

So to connect to the website you have to askfor a permission android manifest here and you say uses-permission android internet,OK.

So then you can use the internet and connectto the website.

So go to the main activity here OK, this isthe.

You know the basic code here you just edit in here.

so in the layout you wantto have like log-in for e-mail and password.

There are two file for you.

The activity main here is for the button thefloating button and the content main is a place you want to add the you know like thetextbox anything here.

So delete it, OK.

Zoom in a little bit and you need an e-mail,grab e-mail like this, OK.

And a password and you need a checkbox becauseyou want to remember your password here being dragged like this.

And the last one is a button for the log-inso rename it I call it etEmail username or password, e-mail it's up to you.

And here I call remember me and cbRemembercheck.

I want to check as a default.

And LogIn etOnLoginand then you want to remove this button you got to activity main and just simply deletelike this.

But you have to delete the code in here aswell here.

And if you don't want a menu, this is themenu code, just delete it.

OK.

So run first to test it.

Here you can give a username or maybe e-maillike this and a password maybe 123 and you remember this and log-in and you go to anotherpage and after that you log-in again the log-in page will be skipped because you rememberedthe password.

OK, this is the scenario.

So first you have to know how to get the datafrom the webpage so I add the library here as you can see before the generic_asynctask_v2if you open here you will see PostResponseAsyncTask, this is the main class and you will querythat back from AsyncResponse class.

So here postResponseAsyncTask we call it tasknew postResponseAsyncTask.

The first one is the context so this and youwant to put the data we can use the third constructor.

If you don't have anything to parse just usethe first one.

so this is the class new you press CTRL+Space and you will see AsyncResponselike this.

And over here you can get the data insidehere, OK.

Say and then you want to point to a URL.

So now I want to use the local one so firstwe will have to have you know like a WAMPP or XAMPP for me I use XAMPP and I have torun it and port is 80.

If you use another port we have to specifyit here.

Because you want to use the local one usuallysay like this http://localhost/.

Usually this is the method you want to connectto the local.

But the local-host here is inside the genymotionor emulator as well.

So if you call local-host it will not connectto your machine local-host.

task.

Execute("http://localhost/"); Soat this point you have to give the alias IP address it's 10.

0.

2.

2 if you are using theandroid 2 emulator but if you use genymotion like me you can use 3 instead.

And if you are using another port beside 80you have to tell Let's say you call 8080 like this than you would say http://10.

0.

3.

2:8080and if you have another page it's for me customer so I have to give to the correct location.

So at this point you have to have a PHP page.

So now you go to htdocs folder[?8:33] andfor [8:34] there is one problem as well.

Usually you cannot easily connect to the 10.

0.

3like this because it has some permission like a config file and then you have to changesomething.

If you don't change it you cannot connect.

You can go to the google and say WAMP permissiondenied like this there are a lot of people that have the same problem and to do thatyou see c:wampaliasphpmyadmin.

Conf you have to change from this to this.

The thing is here OK.

You can change something like that or youcan google for more but if you are using XAMPP like me, you are good to go.

You don't have any problem.

Now go to htdocs I have a PHP here under thefolder customer so maybe just test the local first so this is the current root.

You should go to your browser and say local-host.

Ok this is the local-host xampp like thiscopy.

Like this.

And remember there's a lot of problems.

At first you have to figure out be seeingthe log.

So Let's see it.

Now simply OK.

But Let's say you don't have any internetor maybe your web server is down.

Stop all like this.

Test it again.

OK, as you can see here it said: "Failedto connect to 10.

0.

3.

2 after 15ms" So you have to see this so make sure you havethe good internet and the web-server must be on so start again, and the port must beright.

So now I can connect to the customer.

And in my customer I have the index.

Php pagefor that so Let's see the code first.

We can open it in any text-editor.

For now I use brackets.

it's a open-source.

So this is the log-in page actually if yougo to local-host customer it is the index.

Php OK.

Because I have already written the code soI will explain a little bit.

For example the isset it means that you wanttest it is the txtUsername or txtPassword will come in and on not? and come in fromthe $_POST.

$_POST it means that the data you will notsee from the form here the $_POST and then you grab that and assign to username and thenselect the data from a database.

The database here is from connection and Iwill post the source code in the description of the youtube so you can download the fullsource-code from there.

So if you're using the different database,for example here I use customer and you just change to your database and Let's go to thedatabase here.

This is the database customer.

I have two tables.

The first one is tbl_user for the log-in,this is what we are going to do with so kosal and kosal, abc and 1 2 3 and this one is astring.

OK? This is the AUTO_INCREMENT so nothing specialhere.

And if you want to compare it like exactlythe same you can use binary.

So if the result is greater than one it meansthat you have the data in the database.

It means correct username and password thenyou echo success.

Then you grab the success and check in the website.

The exit here it means that you exit, youdon't want the HTML below to appear.

So this is log-in and kosal.

kosal log-in.

This is the word success.

You grab the success and then you test it in your android.

OK.

This is the URL to the index page so I willlog this.

So I will declare: final String TAG = this.

GetClass().

GetName(); this will give a name here.

We can just say MainActivity but this onewill grab the name and if the name change then the text will change to the same name.

OK?.

And easy to do a log: Log.

D(TAG, s); We run it.

OK.

As you can see this is the result but it isin html.

The html it is from here OK? And you can see the code when you just log-inright-click and View Page Source this is the code.

OK? it's not logged in yet.

So you want to pass the data to the form.

OK.

So you remember the php here you have thetextUsername and textPassword so you can put the data over there.

So here we call: HashMap data = new HashMap(); and then.

so because the hashMap use thekey and value here so you can put the key.

So what is the key here the key is the variableof here in the $_POST so you have to copy if you are not sure and then this one as well.

So this is the data for that.

So Let's say kosal ok data.

Put("txtUsername", "kosal");data.

Put("txtPassword", "kosal"); the password is kosal.

OK.

And you will see something different fromthe before but here we give the data as a second parameter here and new maybe enterthis.

And log here because we put the TAG as a nameof the class.

Let's say MainActivity ok.

see? OK.

The problem that you see is because that Ididn't put the index.

Php at the end.

Remember this is not a redirect page so itdoesn't know if you put you know the customer like this it will redirect to the index.

Php.

It doesn't know so you have to give the exactURL to the php page.

So now you get the word success here.

So after you get the word success you cando something else.

OK.

If s equal and you cannot put the equal signfor the string in java so success.

OK.

Sometime the word equal here can have someproblem as well because sometime the success has a space at the end so I suggest you touse contains like that.

It contains success then you can go to thesubpage for example I have to create a sub-page here.

Right-click, you go to new activity,blank activity, sub-activity for this you see something like this just click the refreshbutton it should disappear.

OK, now you wanna go to the sub so you callIntent in new Intent from MainActivity this SubActivity class and startActivity(in) thenit should go to the sub when it says successful.

Test it again.

OK.

Now you can go to the sub because it is akosal and kosal the right password.

But you want to input the data so you go tocontent_main.

the one that we have the interface here so remember the etEmail, password, buttonand check remember here so you have to get it here first: Button btnLogin;EditText etEmail, etPassword; CheckBox cbRemember; And maybe over here you call it by find youby id and cast to button and assign to the btnLogin.

btnLogin = (Button)findViewById(R.

Id.

BtnLogin);.

And you can do the same for the other OK.

So, now you can get the data so you want toget the data from the e-mail and password here so right here you wanna call maybe Loginlike this.

btnLogin.

SetOnClickListener(this); and then make mainACtivityImplement onClickListener.

It will open onClick method for you so cutall code from here paste it here.

OK.

So now you wanna get the data from etMail.

GetText().

ToString()etPassword.

GetText().

ToString().

Data.

Put("txtUsername", etEmail.

GetText().

ToString()); data.

Put("txtPassword", etPassword.

GetText().

ToString()); OK.

Test it again.

Give a wrong password e-mail first log-in.

Nothing.

We can have message like log-in failed.

Try again here OK with the correct passwordOK.

Now you can go to the sub-activity it's thecorrect one so we have completed a first one so we will go to SharedPreferences to savethe data and use it as like a remember me.

OK.

So over here.

so maybe.

over here you havethe call SharedPreferences interface and maybe you call pref here for short and you get asharedPreferences and the first one is the file name and maybe I call it "login.

Conf"and the mode the context must be MODE_PRIVATE you have to use MODE_PRIVATE.

You don't want to expose your log-in [21:29]to another app it's very important.

SharedPreferences pref = getSharedPreferences("login.

Conf",Context.

MODE_PRIVATE); And SharedPreferences.

Editor editor = pref.

Edit(); then you get the data from there.

Let me show you the example first.

Edithere you can put string now it's a key in value similar to this one.

So you give thekey.

What is the key here I say username and value is the etEmail we just grab it likethis and the password similar etPassword.

after you put all the string you have to callapply to save it editor.

Apply();.

Let's say it save any data and you wantto grab it back you have to call pref.

GetString() ok then you have to give a key a correct oneso username and a second one is a default value so if you save something it means thatthe data username was not saved then it will give the default one now so now I give a defaultas a blank one and password so Let's log it Log.

D(TAG, pref.

GetString("username","")); Log.

D(TAG, pref.

GetString("password","")); OK.

Because the etEmail and password did not applyyet so it's like a blank one.

so maybe I have to put it inside of onClick here so.

maybeI put it over here but now we got a problem the pref here is not belong (doesn't belong)to this method so I have to declare it outside so I cut and this one as well delete it likethis ok s I put it inside onClick so now even the right or wrong password it will save thedata because just for testing.

OK.

Let's say 1 2 3, 1 2 3 now the data is 1 23, 1 2 3.

OK, now Let's say the [24:39] you write ithere is the right thing so Let's comment it here it means that you get a data from thefile should get the data 1 2 3, 1 2 3 from the log-in here even though we don't put astring now nothing but click here see is 1 2 3, 1 2 3.

OK.

Now you got the idea of how to use the SharedPreferences, it's not that hard.

Maybe after you log-in success you want tosave it.

And maybe you just delete this one.

But remember when we log-in only you checkthis if you don't check this it will ask you to enter username and password again so thecheckbox here.

you have to have an event so you set cbRemember.

SetOnCheckboxChangedListener(this);.

MakeMainImplement it so we will createanother method for that.

This is the checked, OK? If it is checked then it's checked, if itis not then it's not.

So easy right? it's check.

it's a boolean.

So you want to get the data so now I haveto declare a boolean outside and I call it checkFlag.

so the flag here is like trueor false.

So first I wanna get the data of the checkFlagfrom the cbRemember is checked , ok? So.

checkflag = cbRemember.

IsChecked();.

It will return the data the default datathat we put in here.

If we put it like unchecked so the defaultwill be unchecked.

Now is checked then it's really true.

OK, so if the checkFlag is true so now wehave to get the data from here as well checkFlag = isChecked;.

We change and then we will change the flagkey as well.

Now test it first.

I don't want to confuse you.

So maybe here, Log.

D(TAG, "checkFlag: " + checkFlag); and because it's a boolean it will be likethis.

and here as well.

OK.

Let's see the log here when I uncheck see? Now the checkFlag is false.

First one is true because of the here ok? So Let's say I uncheck it and I run it again,as you can see now the checkFlag is false as is the first one.

If I check it then it will be true.

OK? This is the checkFlag.

it's very important because we will use thecheckFlag over here and we will say.

if checkFlag is true but for a boolean you don't have totake with the double equalities, you just like just.

If checkFlag is true then youwant to save.

And if not you don't save.

So just all you have to do.

OK.

Let's say 1 2 3 and like this and we say checkok? Now it's false right? it's not gonna saveanything and if you say kosal and password kosal like this it will save, ok? Now you wanna test if it's saved or not.

Now you go to sub here.

you want to use thesame class because you want to choose the same cache So, what you want is just thisline.

You don't have to use the editor.

Editor is for writing.

And over here pref getSharedPreferences defaultname must be the same.

it's better to copy and paste here.

pref = getSharedPreferences("login.

Conf",Context.

MODE_PRIVATE);.

Now log again so copy the log-in then youcan.

Log.

D(TAG, and now pref.

GetString so.

Maybe username like this and the defaultone we leave blank and the password as well Log.

D(TAG, pref.

GetString("username","")); Log.

D(TAG, pref.

GetString("password","")); OK.

a b c, 1 2 3, and you don't remember.

Now it is the last data.

It didn't save.

OK it is correct.

So.

here Let's run it again.

Let's say I check "Remember Me", abc, 123, checked, login Now abc, 123 so it is the correct one.

So when you have the data saved, over here you want to test that if the data is saved then you wanna check it.

If it is the correct password then youdon't have to you know enter the username and password again.

So around here you haveto test it if Let's say we get the data or sorry maybe not this one.

if pref.

GetStringand username, ok? Maybe like this but maybe it's better to assign to String.

maybe it'sbetter to do it outside like this and string password.

so you have to check if it is a blankone then it's not correct, if it is not blank Maybe there is some data.

So you have to check if it is not blank.

it's easy:if(!(username.

Equals("")) && password.

Equals("") It just means that if username and password are not blank, there must be something saved in here.

But why you have to check it like this? Because the username and the password in the database might be changed somewhere else.

For example like you are using computer and you change the password And the app should check it again, you know.

So this is the best way to do that.

So you have to check it here.

And you have to call PostAsyncTask again So it's almost the same.

Just copy here.

And the HashMap code as well.

And you wanna pass in the username and password that have been saved in the SharedPreferences You give the data like this.

But now you don't wanna save anything But you wanna check the data in SharedPreferences aganst the data in database If it is correct, it will return "success" If it is success, you will go into Intent.

Now go here and click (RUN) As you can see, it will skip the login page and go to SubActivity here.

Because it checked the data over here, and send it here.

So this is the correct one, you can do like.

know.

maybe you can have a flash-screen youknow.

and then check the username and password and if not you will go the log-in, if thecorrect one maybe skip the log in and go to the sub one.

So it is your way now I justgive the basic understanding.

OK.

Now in the sub one you want to clear and you want toyou know log out.

So in the sub menu maybe come down here, you can have a button to logout.

But because now we have the button here it's a acitivySub here so I wanna click onthis button and I wanna log out.

So first I wanna change the button Let's call it maybeinputDelete.

if not the right one if you wanna a good image, icon maybe go and downloadfrom the internet.

But now just use this one.

So Let's call it fabLogout and then insidethe sub-activity here fabLogout FloatingActionButton fabLogout = (FlatingActionButton)findViewById(R.

Id.

FabLogout); fabLogout.

SetOnClickListener((view) ? {} So, over here I want to clear all the dataso maybe it's better to call it before the floating, OK.

So, pref here you call editif you wanted to clear edit it's ok, if not just call edit like this and clear and commit.

It will clear all the data in the login.

Conf.

OK? it's a shortcut.

If you want full codeit's like this SharedPreferences.

Editor editor;.

It's better to call it outside and then editor= pref.

Edit() and editor.

Clear, editor.

Comit.

Editor = pref.

Edit();editor.

Clear(); editor.

Commit(); And after you clear it you wanna go back tothe main one.

so Intent in = new Intent(SubActivity.

This, MainActivity.

Class);startAcitivity(in); Ok, so it should go back if you click thebutton.

As you can see now it skipped log-in and go to sub but Let's say log-out now, youcan see the log-in page again.

Let's run it again.

The data should be cleared.

Not Let'ssee the log even though you put the main here nothing.

And the checkFlag is false Let'ssay correct password kosal and kosal and log int now if you don't check it to test it.

Ok.

Go out do the start press it again see it's correct one and check, log in sub, notit's skipped to the sub-activity.

OK? it's almost done except the MD5.

The reasonwhy we have to use the MD5 here is that the log-in username and password is very important.

We should you know encrypt.

and you can just do it by using my library MD5 Simply.

Soyou go there to the MD5 Simply and grab the MD5 Simply here.

Actually we copied alreadyso don't need it.

So to it the MD5 Simply is very easy here when you put the passwordhere, the save here, we'll call MD5 and then encrypt.

that's all you need to do.

OK.

editor.

PutString("password", MD5.

Encrypt(etPassword.

GetText().

ToString()); Here is encrypt.

But Let's test it beforewe do anything.

What is the code here after encrypt? Log.

D(TAG, pref.

GetString("password","")); So kosal, kosal, check, log in.

Now this theMD5 code, ok? So you have to go here and check now you check it but remember this is thecheck, OK? So Let's see if you don't put the MD5 over here just only when you save.

Itdoesn't know because the MD5 code and the code that we enter from website is wrong.

OK? So what you do here is you go to the php and you have to encrypt the password as wellas the MD5.

The username here is not MD5'ed so we cannot just when you insert the datawe have to insert it with the MD5.

So we create the insert one.

this is the insert page butinsert for the product.

Just copy.

Let's call it we insert user so paste the code,that's right, and here input password and txtUsername, txtPassword ok.

Like this.

Andhere you have to check username and txtPassword and delete the rest.

OK.

Something like this.

Good.

name password.

User.

Ok.

And the uid is AUTO_INCREMENT so we don't need it.

Hereit's password and must be a single quote for string.

Delete the rest.

OK.

Should be good.

So delete all of this.

But when we insert it the password has to change to the MD5 aswell so call it insertuser.

Php.

Now Let's call it kosalgeek and password is kosalgeek,insert.

Now insert successfully.

Go to the database and check it out.

Pressed.

Now theMD5 is inserted.

OK.

So when I compare the data from the database and here it must becorrect, OK? Maybe one more thing over here.

We have to put that MD5.

Encrypt() should be finenow.

OK.

Geek, kosalgeek, log in, yes now I don't remember so it should not be saved.

OK.

it's good again.

OK.

It saved.

Log in again.

OK.

As you can see now it skipped tothe sub-activity, it means that the password is correct and we log out.

Should clear allthe data.

OK.

This is the way.

So let me summarize it.

So, first you have to have a php pagefor the insert user.

And make sure that you have the MD5 when you put the username tothe database.

When you check the data here it's ok.

don't put the MD5 but when you checkthe data by enter from edit text here you have to check with the MD5.

And when the checkboxis ticked then you want to add the data and you encrypt with the MD5 as well.

OK.

This is the end of the video.

So if youlike it please don't forget to subscribe my channel and I will see you in the next goodvideo.

Thank you.



Thus Article Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016

That article Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016 This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016 the link address http://svedda79.blogspot.com/2016/04/latest-sharedpreferences-android-studio.html

0 Response to "Latest SharedPreferences Android Studio Tutorial – Remember Me Login, PHP, MySQL, MD5 - 2016"

Post a Comment