2010-06-17 69 views
4

如何在設備內存中存儲用戶名密碼。 即使在用戶關閉應用程序並返回後,他仍應能夠驗證其用戶名和密碼。 現在我正在Eclipse中測試...所以請幫助我一些指針/鏈接,這將允許我在Eclipse中測試並最終在移動版上運行。如何在設備內存中存儲用戶名密碼

回答

1

我已經使用SharedPreferences來存儲用戶名/密碼,它是一個輕量級的存儲解決方案,並保護您的應用程序。

Shared Preferences in Android Dev Guide

+1

SharedPreferences是不安全的!檢查了這一點:https://stackoverflow.com/questions/9244318/android-sharedpreference-security – Spipau 2016-04-15 21:18:45

-1

嘿..我做了d簡單的演示爲u到保存的用戶名密碼ND .. !!! 它存儲到設備的內存中, 它會創建一個文件,並通過/從該文件保存/提取數據.. 代碼如下..

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Login extends Activity { 

    LinearLayout lymain; 

    EditText user_edit; 
    EditText pass_edit; 

    TextView user_txt; 
    TextView pass_txt; 

    CheckBox savepass; 

    Button save; 

    public static final String PREFS_NAME = "MyPrefsFile"; 
    private static final String PREF_USERNAME = "username"; 
    private static final String PREF_PASSWORD = "password"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     lymain = new LinearLayout(getApplicationContext()); 
     lymain.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     lymain.setPadding(15,15,15,15); 
     lymain.setOrientation(1); 
     lymain.setGravity(Gravity.CENTER); 

     user_edit = new EditText(getApplicationContext()); 
     user_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

     pass_edit = new EditText(getApplicationContext());   
     pass_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

     user_txt = new TextView(getApplicationContext()); 
     user_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));   
     user_txt.setText("Enter Username"); 

     pass_txt = new TextView(getApplicationContext()); 
     pass_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     pass_txt.setText("Enter Password"); 

     savepass = new CheckBox(getApplicationContext()); 
     savepass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,40)); 
     savepass.setText("Save Username/Password?"); 

     save = new Button(getApplicationContext()); 
     save.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
     save.setText(" SAVE "); 

     save.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String loginName = user_edit.getText().toString(); 
       String password = pass_edit.getText().toString(); 

       if(savepass.isChecked()){ 
        getSharedPreferences(PREFS_NAME,MODE_PRIVATE) 
         .edit() 
         .putString(PREF_USERNAME, loginName) 
         .putString(PREF_PASSWORD, password) 
         .commit(); 
        Toast.makeText(getApplicationContext(),"Saved Successfully",Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 


     SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); 
     user_edit.setText(pref.getString(PREF_USERNAME, null)); 
     pass_edit.setText(pref.getString(PREF_PASSWORD, null)); 


     lymain.addView(user_txt); 
     lymain.addView(user_edit); 
     lymain.addView(pass_txt); 
     lymain.addView(pass_edit); 
     lymain.addView(savepass); 
     lymain.addView(save); 

     setContentView(lymain);  
    } 
} 
+1

我不建議將密碼存儲到SharedPreferences由於安全原因,也許根植手機可以閱讀這些選項,它可能是有用的使用AccountManager保存密碼或類似的東西。 – ziniestro 2014-12-16 01:06:57