我知道共享首选项的工作方式,但是我只是不知道在下面的代码中将代码插入哪里以保存用户数据.我有一个登录,后台任务来完成所有工作和注册.我希望该应用程序能够大放异彩用户登录时的页面.无法从其他答案中找出答案
Login.java
button bttnLogin;EditText loginEmail, loginPassword;TextVIEw reglink;AlertDialog.Builder alert;@OverrIDeprotected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); loginEmail = (EditText) findVIEwByID(R.ID.email); loginPassword = (EditText) findVIEwByID(R.ID.password); reglink = (TextVIEw) findVIEwByID(R.ID.reglink); reglink.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { startActivity(new Intent(Login.this, Register.class)); } }); bttnLogin = (button) findVIEwByID(R.ID.bttnLogin); bttnLogin.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (loginEmail.getText().toString().equals("") || loginPassword.getText().toString().equals("")) { alert = new AlertDialog.Builder(Login.this); alert.set@R_301_5979@("Login Failed"); alert.setMessage("Try again"); alert.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alertDialog = alert.create(); alertDialog.show(); } else //if user provIDes proper data { BackgroundTask backgroundTask = new BackgroundTask(Login.this); backgroundTask.execute("login", loginEmail.getText().toString(), loginPassword.getText().toString()); } } });
Register.java
private button regbutton; public EditText regname; public EditText regEmail; public EditText regPassword; public EditText conPassword; private AlertDialog.Builder alert;@OverrIDeprotected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_register); regname = (EditText) findVIEwByID(R.ID.name); regEmail = (EditText) findVIEwByID(R.ID.email); regPassword = (EditText) findVIEwByID(R.ID.password); conPassword = (EditText) findVIEwByID(R.ID.conPassword); regbutton = (button) findVIEwByID(R.ID.regbutton); regbutton.setonClickListener(this);}@OverrIDepublic voID onClick(VIEw v){ if (regname.getText().toString().equals("") || regEmail.getText().toString().equals("") || regPassword.getText().toString().equals("") || conPassword.getText().toString().equals("")) { alert = new AlertDialog.Builder(Register.this); alert.set@R_301_5979@("Something not quite right"); alert.setMessage("Please fill in all the fIElds"); alert.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alertDialog = alert.create(); alertDialog.show(); } else if (!(regPassword.getText().toString().equals(conPassword.getText().toString()))) { alert = new AlertDialog.Builder(Register.this); alert.set@R_301_5979@("Passwords do not match"); alert.setMessage("Try Again"); alert.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); conPassword.setText(""); regPassword.setText(""); } }); AlertDialog alertDialog = alert.create(); alertDialog.show(); } else //if user provIDes proper data { BackgroundTask backgroundTask = new BackgroundTask(Register.this); backgroundTask.execute("register", regname.getText().toString(), regEmail.getText().toString() , regPassword.getText().toString(), conPassword.getText().toString()); }
BackgroundTask.java
public class BackgroundTask extends AsyncTask<String,VoID,String>{private Context context;private Activity activity;private String reg_url = "http://blaah.com/register.PHP";private String login_url = "http://blaah.com/login.PHP";private AlertDialog.Builder builder;private ProgressDialog progressDialog;public BackgroundTask(Context context){ this.context = context; activity = (Activity) context;}@OverrIDeprotected voID onPreExecute(){ builder = new AlertDialog.Builder(activity); progressDialog = new ProgressDialog(context); progressDialog.set@R_301_5979@("Please wait"); progressDialog.setMessage("Connecting to Server..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(false); progressDialog.show();}@OverrIDeprotected String doInBackground(String... params){ String method = params[0]; if (method.equals("register")) { try { URL url = new URL(reg_url); httpURLConnection httpURLConnection = (httpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoinput(true); OutputStream outputStream = httpURLConnection.getoutputStream(); BuffereDWriter buffereDWriter = new BuffereDWriter(new OutputStreamWriter(outputStream, "UTF-8")); String name = params[1]; String email = params[2]; String username = params[3]; String password = params[4]; String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); buffereDWriter.write(data); buffereDWriter.flush(); buffereDWriter.close(); outputStream.close(); inputStream inputStream = httpURLConnection.getinputStream(); BufferedReader bufferedReader = new BufferedReader(new inputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = ""; while ((line = bufferedReader.readline()) != null) { stringBuilder.append(line).append("\n"); } httpURLConnection.disconnect(); Thread.sleep(8000); return stringBuilder.toString().trim(); } catch (MalformedURLException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } catch (InterruptedException e) { e.printstacktrace(); } } else if (method.equals("login")) { try { URL url = new URL(login_url); httpURLConnection httpURLConnection = (httpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoinput(true); OutputStream outputStream = httpURLConnection.getoutputStream(); BuffereDWriter buffereDWriter = new BuffereDWriter(new OutputStreamWriter(outputStream, "UTF-8")); String username,password; username = params[1]; password = params [2]; String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); buffereDWriter.write(data); buffereDWriter.flush(); buffereDWriter.close(); outputStream.close(); inputStream inputStream = httpURLConnection.getinputStream(); BufferedReader bufferedReader = new BufferedReader(new inputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = ""; while ((line = bufferedReader.readline()) != null) { stringBuilder.append(line + "\n"); } httpURLConnection.disconnect(); Thread.sleep(5000); return stringBuilder.toString().trim(); } catch (MalformedURLException e) { e.printstacktrace(); } catch (ProtocolException e) { e.printstacktrace(); } catch (UnsupportedEnCodingException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } catch (InterruptedException e) { e.printstacktrace(); } } return null;}@OverrIDeprotected voID onProgressUpdate(VoID... values){ super.onProgressUpdate(values);}@OverrIDeprotected voID onPostExecute(String Json){ try { progressDialog.dismiss(); Log.v("JsON", Json); JsONObject JsonObject = new JsONObject(Json); JsONArray JsonArray = JsonObject.getJsONArray("server_response"); JsONObject Jsonobject = JsonArray.getJsONObject(0); String code = Jsonobject.getString("code"); String message = Jsonobject.getString("message"); if(code.equals("reg_true")) { showDialog("Sucessful registration.Thank you.Enjoy_AS!", message, code); } else if (code.equals("reg_false")) { showDialog("User Already exists", message, code); } else if (code.equals("login_true")) { Toast.makeText(context, "You are logged in", Toast.LENGTH_LONG).show(); Intent intent = new Intent(activity, SplashScreen.class); activity.startActivity(intent); } else if (code.equals("login_false")) { showDialog("Login Error", message, code); } } catch (JsONException e){ e.printstacktrace(); }}public voID showDialog(String @R_301_5979@,String message,String code){ builder.set@R_301_5979@(@R_301_5979@); if (code.equals("reg_true") || code.equals("reg_false")) { builder.setMessage(message);//message form server builder.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { dialog.dismiss(); activity.finish(); } }); } else if (code.equals("login_false")) { builder.setMessage(message); builder.setPositivebutton("OK", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { EditText loginEmail, loginPassword; loginEmail = (EditText) activity.findVIEwByID(R.ID.email); loginPassword = (EditText) activity.findVIEwByID(R.ID.password); loginEmail.setText(""); loginPassword.setText(""); dialog.dismiss(); } }); } AlertDialog alertDialog = builder.create(); alertDialog.show();}}
我想打开应用程序的启动画面
final String TAG = this.getClass().getname();private static int SPLASH_TIME_OUT = 4000;private TextVIEw saying;@OverrIDeprotected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //this.requestwindowFeature(Window.FEATURE_NO_@R_301_5979@); supportRequestwindowFeature(Window.FEATURE_NO_@R_301_5979@); getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN, WindowManager.LayoutParams.FLAG_FulLSCREEN); setContentVIEw(R.layout.activity_splash_screen); saying = (TextVIEw) findVIEwByID(R.ID.saying); Typeface mainhead = Typeface.createFromAsset(getAssets(), "Fonts/emporo.TTF"); saying.setTypeface(mainhead); //Set text custom Font for subhead text new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { // This method will be executed once the timer is over // Start your app main activity Intent i = new Intent(SplashScreen.this, HomeScreen.class); startActivity(i); // close this activity finish(); } }, SPLASH_TIME_OUT);}}
解决方法:
这就是我在每个AndroID应用中使用的SharedPreferences
import androID.content.Context;import androID.content.SharedPreferences;/** * Created by Jimit Patel on 30/07/15. */public class Prefs { private static final String TAG = Prefs.class.getSimplename(); private static final String MY_APP_PREFS = "my_app"; /** * <p>ProvIDes Shared Preference object</p> * @param context * @return */ private static SharedPreferences getPrefs(Context context) { return context.getSharedPreferences(MY_APP_PREFS, Context.MODE_PRIVATE); } /** * <p>Saves a string value for a given key in Shared Preference</p> * @param context * @param key * @param value */ public static voID setString(Context context, String key, String value) { getPrefs(context).edit().putString(key, value).apply(); } /** * <p>Saves integer value for a given key in Shared Preference</p> * @param context * @param key * @param value */ public static voID setInt(Context context, String key, int value) { getPrefs(context).edit().putInt(key, value).apply(); } /** * <p>Saves float value for a given key in Shared Preference</p> * @param context * @param key * @param value */ public static voID setfloat(Context context, String key, float value) { getPrefs(context).edit().putfloat(key, value).apply(); } /** * <p>Saves long value for a given key in Shared Preference</p> * @param context * @param key * @param value */ public static voID setLong(Context context, String key, long value) { getPrefs(context).edit().putLong(key, value).apply(); } /** * <p>Saves boolean value for a given key in Shared Preference</p> * @param context * @param key * @param value */ public static voID setBoolean(Context context, String key, boolean value) { getPrefs(context).edit().putBoolean(key, value).apply(); } /** * ProvIDes string from the Shared Preferences * @param context * @param key * @param defaultValue * @return */ public static String getString(Context context, String key, String defaultValue) { return getPrefs(context).getString(key, defaultValue); } /** * ProvIDes int from Shared Preferences * @param context * @param key * @param defaultValue * @return */ public static int getInt(Context context, String key, int defaultValue) { return getPrefs(context).getInt(key, defaultValue); } /** * ProvIDes boolean from Shared Preferences * @param context * @param key * @param defaultValue * @return */ public static boolean getBoolean(Context context, String key, boolean defaultValue) { return getPrefs(context).getBoolean(key, defaultValue); } /** * ProvIDes float value from Shared Preferences * @param context * @param key * @param defaultValue * @return */ public static float getfloat(Context context, String key, float defaultValue) { return getPrefs(context).getfloat(key, defaultValue); } /** * ProvIDes long value from Shared Preferences * @param context * @param key * @param defaultValue * @return */ public static long getLong(Context context, String key, long defaultValue) { return getPrefs(context).getLong(key, defaultValue); } public static voID clearPrefs(Context context) { getPrefs(context).edit().clear().commit(); }}
要使用它,您可以只使用它的那些功能
总结以上是内存溢出为你收集整理的android-共享的首选项,它们在哪里?全部内容,希望文章能够帮你解决android-共享的首选项,它们在哪里?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)