Setting Sessions in Android Application
Android में किसी
एप्लीकेशन के data को store करने के विभिन्न तरीके हैं और उनमे सबसे अच्छा और आसान
तरीका Shared
Preferences हैं . Shared Preferences के द्वारा आप key /Value Pair के रूप में data को save
कर सकते हैं.
अब Shared Preferences को उसे केसे करें तो इसके लिए आपको getSharedPreferences()
method को call करना होगा ये SharedPreference का एक instance return करेगा जो उस फाइल को point करेगा
जिसमे Preference की
value होगी
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
पहला parameter (MyPREFERENCES) key है और दूसरा (Context.MODE_PRIVATE) mode है. private
के अलावा कुछ और modes भी उपलब्ध हैं जेसे -:
- MODE_APPEND
- MODE_ENABLE_WRITE_AHEAD_LOGGING
- MODE_MULTI_PROCESS
- MODE_WORLD_READABLE
- MODE_WORLD_WRITEABLE
MODE_PRIVATE के द्वारा आप ये सुनिश्चित करते हैं की preference को केवल आप ही
की application read कर सके. यदि आप इसे दुसरे application के लिए भी उपलब्ध करना
कहते हैं तो MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE का उपयोग कर सकते हैं.
MODE_WORLD_READABLE अन्य को केवल पड़ने हेतु उपलब्ध कराएगा जबकि MODE_WORLD_WRITEABLE लिखने हेतु.
अब आप इस sharedpreferences में कुछ
save कर सकते हैं उसके लिए आपको Editor class को use करना होगा. sharedpreferences.edit() function आपको Editor class उपलब्ध करेगा इसको
निम्न प्रकार से use कर सकते हैं.
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
कमिट से आप
सुनिश्चित करते हैं की आप की value save हो गयी.
putString के अलावा भी
अन्य methods है जो जिनको इस्तेमाल किया जा सकता है जेसे
- apply()
- clear()
- remove(String key)
- putLong(String key, long value)
- putInt(String key, int value)
- putFloat(String key, float value)
Example
Source File
public class Login extends Activity {
Activity
context;
protected
EditText username;
private EditText password;
protected TextView myreg;
String enteredPassword;
protected String enteredUsername;
SharedPreferences sharedpreferences;
String UName;
public static final String MyPREFERENCES = "MyPrefs" ;
private EditText password;
protected TextView myreg;
String enteredPassword;
protected String enteredUsername;
SharedPreferences sharedpreferences;
String UName;
public static final String MyPREFERENCES = "MyPrefs" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context=this;
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
Button loginButton = (Button)findViewById(R.id.submit);
loginButton.setOnClickListener(new View.OnClickListener() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context=this;
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
Button loginButton = (Button)findViewById(R.id.submit);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(Home.this, "Welcome", Toast.LENGTH_LONG).show(); enteredUsername = username.getText().toString();
enteredPassword = password.getText().toString();
if(enteredUsername.equals("") || enteredPassword.equals("")){
public void onClick(View v) {
//Toast.makeText(Home.this, "Welcome", Toast.LENGTH_LONG).show(); enteredUsername = username.getText().toString();
enteredPassword = password.getText().toString();
if(enteredUsername.equals("") || enteredPassword.equals("")){
Toast.makeText(Login.this, "Username or password must be
filled", Toast.LENGTH_LONG).show();
return;
}
return;
}
if(enteredUsername.length() <= 1 ||
enteredPassword.length() <= 1){
Toast.makeText(Login.this,
"Username or password length must be greater than one",
Toast.LENGTH_LONG).show(); return;
}
}
/************************************************
Code
to retrieve correct password from database
**********************************************************/
if(validuser)
{
setUname(enteredUsername);
}
});
}
{
setUname(enteredUsername);
}
});
}
public void setUname(String uname)
{
sharedpreferences =PreferenceManager.getDefaultSharedPreferences(applicationContext);
SharedPreferences.Editor
editor = sharedpreferences.edit();
editor.putString("UName",uname);
editor.commit();
}
}
}
Login Page Layout
<!-- copyrighted content owned by
Android Arena (www.androidarena.co.in)-->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="30dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/greybackground"
>
<ImageView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/cup"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="#f3f3f3"
android:hint="Username"
android:paddingLeft="5dp"
android:singleLine="true"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="15dp"
android:background="#f3f3f3"
android:hint="password"
android:paddingLeft="5dp"
/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#000000"
android:text="Forgot Password?"
android:textStyle="bold"
/>
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="190dp"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:background="#13b586"
android:text="submit"
android:textColor="#fff"
android:textSize="13dp"
/>
</LinearLayout>
</ScrollView>
</ScrollView>
No comments:
Post a Comment