Shared Preferences reset data when app is force closed or device is restarted

后端 未结 6 1887
暖寄归人
暖寄归人 2020-11-30 11:21

I\'m developing an application in which I\'m storing username and password in SharedPreferences. All things are working fine for me, s

6条回答
  •  自闭症患者
    2020-11-30 12:07

    If you want to try an approach other than shared preferences then you can use a file to write the values and then retrieve the values from it. Declare a text file emailLogin.txt and use it as follows to write the data.

               try {
                    Context context = MainActivity.this;
                    OutputStreamWriter out=
                            new OutputStreamWriter(context.openFileOutput(EMAILLOGIN, 0));
    
                    out.write(email_text);                   
                    out.write("\r\n");
                    out.write(userIDToken);                   
                    out.write("\r\n");
                    out.write(accessToken);                   
                    out.write("\r\n");
    
                    out.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
    

    After writing to the file, you can do the following to read from it.

       File file = this.getFileStreamPath(EMAILLOGIN);
    
                if(file.exists())
                {
                    try {
                        InputStream in=openFileInput(NUMBERS);
    
                        if (in!=null) {
                            InputStreamReader tmp=new InputStreamReader(in);
                            BufferedReader reader=new BufferedReader(tmp);
                            String str;
                            String strcount[]= new String[20];                                                        
    
                            java.util.Arrays.fill(strcount, 0, 10, "");
                            while ((str = reader.readLine()) != null) {                       
                                strcount[linecount]=str;                              
                            }
    
                            loginEmail = strcount[0];
                            loginUserId = strcount[1];
                            loginAccessToken = strcount[2];
    
                            in.close();              
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    

    This will ensure that even when your app force closes or if your device reboots, you can still read the required values from the file.

提交回复
热议问题