Created
July 31, 2016 12:54
-
-
Save Bahaaib/74be66bfb26045fe8025e49f90e8d9c6 to your computer and use it in GitHub Desktop.
How to setup a User LogIn / Logout / Signup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.parse.starter; | |
| import android.content.Intent; | |
| import android.database.Cursor; | |
| import android.graphics.BitmapFactory; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.provider.MediaStore; | |
| import android.support.v7.app.ActionBarActivity; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| import android.widget.ImageView; | |
| import com.parse.FindCallback; | |
| import com.parse.GetCallback; | |
| import com.parse.LogInCallback; | |
| import com.parse.Parse; | |
| import com.parse.ParseAnalytics; | |
| import com.parse.ParseException; | |
| import com.parse.ParseObject; | |
| import com.parse.ParseQuery; | |
| import com.parse.ParseUser; | |
| import com.parse.SaveCallback; | |
| import com.parse.SignUpCallback; | |
| import java.util.List; | |
| public class MainActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| ParseUser user = new ParseUser(); //create a new user | |
| user.setUsername("robpercival"); //set user "username" | |
| user.setPassword("myPass"); // set user "password" | |
| user.signUpInBackground(new SignUpCallback() { | |
| @Override | |
| public void done(ParseException e) { // feedback if signning up process gone OK | |
| if (e == null) { | |
| Log.i("signUp", "Successful"); | |
| } else { | |
| Log.i("signUp", "Failed"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| }); | |
| ParseUser.logInInBackground("robpercival", "myPass123", new LogInCallback() { // log in a user with log in parameters | |
| @Override | |
| public void done(ParseUser user, ParseException e) { //Feedback if log in gone OK | |
| if (user != null) { | |
| Log.i("logIn", "Successful"); | |
| } else { | |
| Log.i("logIn", "Unsuccessful"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| }); | |
| ParseUser.logOut(); //log user out | |
| if (ParseUser.getCurrentUser() != null) { // check if user still logged in.. Remember him and keep him logging | |
| Log.i("currentUser", "User logged in"); | |
| } else { | |
| Log.i("currentUser", "Not logged in"); | |
| } | |
| ParseAnalytics.trackAppOpenedInBackground(getIntent()); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.menu_main, menu); | |
| return true; | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| int id = item.getItemId(); | |
| //noinspection SimplifiableIfStatement | |
| if (id == R.id.action_settings) { | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - in Starter application. java file the following line must be commented before writting any code at Main_Activity: | |
| ParseUser.enableAutomaticUser(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment