How to delete SharedPreferences automatically after one month?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to delete SharedPreferences, if they exist, after one month automatically. I could not find any solutions. Is this possible to make?



Thanks a lot.










share|improve this question

























  • look into services and broadcasts. after one month, delete shared pref.

    – soldfor
    Jan 4 at 17:14











  • Use the AlarmManager

    – Rishabh Rawat
    Jan 4 at 17:24


















0















I want to delete SharedPreferences, if they exist, after one month automatically. I could not find any solutions. Is this possible to make?



Thanks a lot.










share|improve this question

























  • look into services and broadcasts. after one month, delete shared pref.

    – soldfor
    Jan 4 at 17:14











  • Use the AlarmManager

    – Rishabh Rawat
    Jan 4 at 17:24














0












0








0








I want to delete SharedPreferences, if they exist, after one month automatically. I could not find any solutions. Is this possible to make?



Thanks a lot.










share|improve this question
















I want to delete SharedPreferences, if they exist, after one month automatically. I could not find any solutions. Is this possible to make?



Thanks a lot.







android android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 17:14









dharms

3,6311224




3,6311224










asked Jan 4 at 17:04









NinaNina

82




82













  • look into services and broadcasts. after one month, delete shared pref.

    – soldfor
    Jan 4 at 17:14











  • Use the AlarmManager

    – Rishabh Rawat
    Jan 4 at 17:24



















  • look into services and broadcasts. after one month, delete shared pref.

    – soldfor
    Jan 4 at 17:14











  • Use the AlarmManager

    – Rishabh Rawat
    Jan 4 at 17:24

















look into services and broadcasts. after one month, delete shared pref.

– soldfor
Jan 4 at 17:14





look into services and broadcasts. after one month, delete shared pref.

– soldfor
Jan 4 at 17:14













Use the AlarmManager

– Rishabh Rawat
Jan 4 at 17:24





Use the AlarmManager

– Rishabh Rawat
Jan 4 at 17:24












3 Answers
3






active

oldest

votes


















0














It depends.



The easiest way is to delete it when the user starts the app.
When the apps is created, you check the SharedPreferences for the last updated time.
If it's null (the first time), you save the current time in milliseconds as a long.
If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.



Something like:



long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
if(lastUpdate == -1) {
//First time
sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
} else {
boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
if(isMoreThanAMonth) {
sharedPreferences.edit().clear().apply()
}
}


Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.






share|improve this answer































    0














    long installed = context
    .getPackageManager()
    .getPackageInfo(context.getPackag‌​eName(), 0)
    .firstInstallTime
    ;


    public long firstInstallTime
    The time at which the app was first installed. Units are as per System.currentTimeMillis().



    Now you can compare two date and get months diffrent by using GregorianCalendar
    after you get one month different do as you want..clear sharedPrefrence.edit().clear().commit()






    share|improve this answer































      0














      One possible way,

      1. get the calendar instance.

      2. Get maximum day of month.

      3. Store in a var1 string in format of dd/mm/yyyy.

      4. Get the current date from some calendar object and store in same way from point 3 but in var2.

      5. Compare two strings.

      6. If match then it will be last day of month and call delete() on your files.

      Done.






      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043238%2fhow-to-delete-sharedpreferences-automatically-after-one-month%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        It depends.



        The easiest way is to delete it when the user starts the app.
        When the apps is created, you check the SharedPreferences for the last updated time.
        If it's null (the first time), you save the current time in milliseconds as a long.
        If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.



        Something like:



        long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
        if(lastUpdate == -1) {
        //First time
        sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
        } else {
        boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
        if(isMoreThanAMonth) {
        sharedPreferences.edit().clear().apply()
        }
        }


        Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.






        share|improve this answer




























          0














          It depends.



          The easiest way is to delete it when the user starts the app.
          When the apps is created, you check the SharedPreferences for the last updated time.
          If it's null (the first time), you save the current time in milliseconds as a long.
          If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.



          Something like:



          long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
          if(lastUpdate == -1) {
          //First time
          sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
          } else {
          boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
          if(isMoreThanAMonth) {
          sharedPreferences.edit().clear().apply()
          }
          }


          Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.






          share|improve this answer


























            0












            0








            0







            It depends.



            The easiest way is to delete it when the user starts the app.
            When the apps is created, you check the SharedPreferences for the last updated time.
            If it's null (the first time), you save the current time in milliseconds as a long.
            If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.



            Something like:



            long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
            if(lastUpdate == -1) {
            //First time
            sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
            } else {
            boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
            if(isMoreThanAMonth) {
            sharedPreferences.edit().clear().apply()
            }
            }


            Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.






            share|improve this answer













            It depends.



            The easiest way is to delete it when the user starts the app.
            When the apps is created, you check the SharedPreferences for the last updated time.
            If it's null (the first time), you save the current time in milliseconds as a long.
            If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.



            Something like:



            long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
            if(lastUpdate == -1) {
            //First time
            sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
            } else {
            boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
            if(isMoreThanAMonth) {
            sharedPreferences.edit().clear().apply()
            }
            }


            Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 4 at 18:02









            Rodrigo Ezequiel Arias RobertsRodrigo Ezequiel Arias Roberts

            836




            836

























                0














                long installed = context
                .getPackageManager()
                .getPackageInfo(context.getPackag‌​eName(), 0)
                .firstInstallTime
                ;


                public long firstInstallTime
                The time at which the app was first installed. Units are as per System.currentTimeMillis().



                Now you can compare two date and get months diffrent by using GregorianCalendar
                after you get one month different do as you want..clear sharedPrefrence.edit().clear().commit()






                share|improve this answer




























                  0














                  long installed = context
                  .getPackageManager()
                  .getPackageInfo(context.getPackag‌​eName(), 0)
                  .firstInstallTime
                  ;


                  public long firstInstallTime
                  The time at which the app was first installed. Units are as per System.currentTimeMillis().



                  Now you can compare two date and get months diffrent by using GregorianCalendar
                  after you get one month different do as you want..clear sharedPrefrence.edit().clear().commit()






                  share|improve this answer


























                    0












                    0








                    0







                    long installed = context
                    .getPackageManager()
                    .getPackageInfo(context.getPackag‌​eName(), 0)
                    .firstInstallTime
                    ;


                    public long firstInstallTime
                    The time at which the app was first installed. Units are as per System.currentTimeMillis().



                    Now you can compare two date and get months diffrent by using GregorianCalendar
                    after you get one month different do as you want..clear sharedPrefrence.edit().clear().commit()






                    share|improve this answer













                    long installed = context
                    .getPackageManager()
                    .getPackageInfo(context.getPackag‌​eName(), 0)
                    .firstInstallTime
                    ;


                    public long firstInstallTime
                    The time at which the app was first installed. Units are as per System.currentTimeMillis().



                    Now you can compare two date and get months diffrent by using GregorianCalendar
                    after you get one month different do as you want..clear sharedPrefrence.edit().clear().commit()







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 4 at 17:33









                    Ashvin solankiAshvin solanki

                    1,682729




                    1,682729























                        0














                        One possible way,

                        1. get the calendar instance.

                        2. Get maximum day of month.

                        3. Store in a var1 string in format of dd/mm/yyyy.

                        4. Get the current date from some calendar object and store in same way from point 3 but in var2.

                        5. Compare two strings.

                        6. If match then it will be last day of month and call delete() on your files.

                        Done.






                        share|improve this answer




























                          0














                          One possible way,

                          1. get the calendar instance.

                          2. Get maximum day of month.

                          3. Store in a var1 string in format of dd/mm/yyyy.

                          4. Get the current date from some calendar object and store in same way from point 3 but in var2.

                          5. Compare two strings.

                          6. If match then it will be last day of month and call delete() on your files.

                          Done.






                          share|improve this answer


























                            0












                            0








                            0







                            One possible way,

                            1. get the calendar instance.

                            2. Get maximum day of month.

                            3. Store in a var1 string in format of dd/mm/yyyy.

                            4. Get the current date from some calendar object and store in same way from point 3 but in var2.

                            5. Compare two strings.

                            6. If match then it will be last day of month and call delete() on your files.

                            Done.






                            share|improve this answer













                            One possible way,

                            1. get the calendar instance.

                            2. Get maximum day of month.

                            3. Store in a var1 string in format of dd/mm/yyyy.

                            4. Get the current date from some calendar object and store in same way from point 3 but in var2.

                            5. Compare two strings.

                            6. If match then it will be last day of month and call delete() on your files.

                            Done.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 4 at 19:13









                            Viraj SViraj S

                            299111




                            299111






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043238%2fhow-to-delete-sharedpreferences-automatically-after-one-month%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas