简体   繁体   中英

Android changed value stored in program

I have a dialog where the user have to write the password. If it is appropriate then will open another a dialog where the user can change some settings. Is there any why to save the changed value in to exactly to the program or I have to write in to file and realoaded from there. But I think it is not so safty if I want to hide this settings from simple users.

Which is the best solutions for that?

As long as you store a hashed version of the password there shouldn't be a problem.

When you check the password you then check against the hashed version.

Whether you choose to store it in a flat file, or database doesn't matter, for me if it was just a couple of users I would be tempted by some config file, if multiple users might use the same device I might use a database. It also depends if my users are going to have other associated data.

You can use SharedPrefernces for this purpose. Or, you can try Storage Options provided by Android.

Edit
This code allows you to enter some text in EditText and when you click Save it will be saved. The saved text would be displayed in EditText when the app is opened again.

MainActivity.java

package pcsalt.example.sharedprefdemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText et;
    Button btn;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                editor = pref.edit();
                String text = et.getText().toString();
                editor.putString("key_value", text);
                editor.commit();
            }
        });

        et.setText(pref.getString("key_value", "default value"));
    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Some text" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM