0

I want to create a sort of session where the user logs in and for an amount of time he doesn't have to type his password everytime he opens the app.

i tried to do something like this , just for test : but if you have any ideas i'd really appreciate it , thanks. Again this is just a test to see if i can modify a stored value after some time.

public class MainActivity extends AppCompatActivity {


public SharedPreferences mPreferenceManager;
public SharedPreferences.Editor mEditor;
private static final int DURATION = 20000;
private long activatedAt = Long.MAX_VALUE;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView hello=(TextView) findViewById(R.id.hello);
    Button but1=(Button) findViewById(R.id.button1);


    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,configActivity.class);
            startActivity(i);
        }
    });

    SharedPreferences setting = getSharedPreferences("settings", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = setting.edit();


    long activeFor=System.currentTimeMillis()-activatedAt;
    if(activeFor>5000){
        editor.clear();
        Log.d("here-",">50000");
    }else{
        editor.putString("pseudo","hello");
        editor.commit();
        Log.d("here-","<50000");
    }
    hello.setText(setting.getString("pseudo",""));

}
apps
  • 21
  • 2
  • please explain instead of downvoting my post – apps Jul 03 '17 at 12:27
  • using service that only perform background options. I don't know about who is click downvote. – Raj Jul 03 '17 at 12:27
  • can you please give me an example, i'm new to this – apps Jul 03 '17 at 12:27
  • https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html – Raj Jul 03 '17 at 12:30
  • Downvoted possibly as this is a common question and multiple questions are alread asked on the subject. To your question, yes you can use sharedpreferences to store and edit any value. You can look at this [link](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) for more info. – Oasa Jul 03 '17 at 12:34
  • but how do i edit it after ,let's say, a value of time? since everytime i leave the app , the currentTime is reinistialized no? – apps Jul 03 '17 at 12:44
  • Hi and welcome to Stack Overflow, please take a time to go through the [welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – DarkCygnus Jul 03 '17 at 15:55
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – DarkCygnus Jul 03 '17 at 15:57

0 Answers0