I am new to android development and i am working with a login activity. So for that i need to create a central database so that all the users can be authenticated and hence logged in. So please tell me how can I do this. Can I use sqlite to create this type of data or it is just used to create a local database per client? Thank you
-
Central database means a database in server.it is the better way to make a login Activity.you can use rest web service to do that – Akhil Jayakumar Oct 21 '15 at 10:04
-
so how should i provide the address of this database kept at my server? – Divyesh Jindal Oct 21 '15 at 10:11
-
you can create API with php/java and upload to your server.then access it via its URL.you cant post your username and password in http post.the server gives a response in JSON/XML format.that response can be parsed and display as status. – Akhil Jayakumar Oct 21 '15 at 10:15
-
@DivyeshJindal for some reason, my old answer (you accepted before) had deleted, you can accept it now :-) – droidev Oct 26 '15 at 03:59
6 Answers
For allowing multiple user loggin in the app you need to use central database .
Below is the link which will help you a lot.
http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
- 5,411
- 4
- 30
- 47
You'd need an API to do this if you want it to span across several users. I'd recommend using Parse as a beginner as it's easy to set up and use.
tutorial:
http://www.androidbegin.com/tutorial/android-parse-com-simple-login-and-signup-tutorial/
- 2,420
- 2
- 15
- 19
used a SQL database instead of sqlite and used sheared preference for local storage. that is the best for create a login activity in any application.
- 609
- 5
- 19
-
-
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ use this tutorial for create an API by using PHP and passs that api url in your code.as done ame in tutorial. that is a best working tutorial – Shivanshu Verma Oct 21 '15 at 10:17
-
sure.. i added sheared preference class too in one more comment here.. if u face any problem feel free to ask – Shivanshu Verma Oct 21 '15 at 10:30
Case 1:
If you have the Central Database in the Server then simply use the SharedPreferences to save login credentials which you getting from Server for individual client device. No need to make the database for it.
Case2:
If you want to make Central Database in the mobile then use the Sqlite to save login credentials.
- 155
- 11
-
i am thinking of case 1. But i don't know how to communicate between the server and my app can u get me some help over that? – Divyesh Jindal Oct 21 '15 at 10:16
-
also how can i implement case 2. i.e central database in mobile because if the data is in the device of a particular device and if the user uninstalls the app all his records would vanish and the next time he downloads the app he has to signup again instead of just login in ? – Divyesh Jindal Oct 21 '15 at 10:18
-
First learn case 1. that is easy read this link [How can I fetch data from a web server in an android application?](http://stackoverflow.com/questions/16545378/how-can-i-fetch-data-from-a-web-server-in-an-android-application) – Android007 Oct 21 '15 at 10:26
-
for sheared prefrence create a class name Session?Manager
public class SessionManager {
// Email address (make variable public to access from outside)
public static final String KEY_api = "apikey";
private static final String KEY_NAME = "name";
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
private static final String push_ = "AndroidHivePref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String name, String apikey) {
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_api, apikey);
// commit changes
editor.commit();
}
public int getMerchentPushCount() {
return pref.getInt("MerchentPushCount", 0);
}
public void setMerchentPushCount(Integer count) {
editor.putInt("MerchentPushCount", count);
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
*/
public void checkLogin() {
// Check login status
if (!this.isLoggedIn()) {
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user api key
user.put(KEY_api, pref.getString(KEY_api, null));
// return user
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* *
*/
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
and called it in LoginActivity class ...
session = new SessionManager(getApplicationContext());
SharedPreferences pref = session.pref;
- 609
- 5
- 19
Please have a look at using PHP with MySQL.
for auto login, I recommend you to keep session in preference other than keeping user id and password.