SharedPreferences doesn't save values









up vote
1
down vote

favorite
1












for some reason SharedPreferences doesn't save the values, the code is in a service...



 Thread thread = new Thread() 
public void run()
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.not_local_sp), MODE_PRIVATE);
long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L);
int scanInterval = 500 * 60 * 60;

while (true)
if (System.currentTimeMillis() - lastExecuted >= scanInterval && contextCreator == null)
Log.d(TAG, "inside while loop");
Log.d(TAG, String.valueOf(System.currentTimeMillis() - lastExecuted));
Log.d(TAG, String.valueOf(lastExecuted));
Log.d(TAG, String.valueOf(scanInterval));
contextCreator = new ProtectionContextCreator();
feature = AppScanFactory.createAppScan(getBaseContext());
feature.add(new BoostFeature(getBaseContext(), new StubScanStore()));
contextCreator.create(getBaseContext(), feature.getType(), NotificationService.this);

/*Shared pref*/

SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(getString(R.string.timeDone), System.currentTimeMillis());
editor.commit();


try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();



;
thread.start();


the lastExecuted variable always get the default value, 0.










share|improve this question







New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • how do you know that is not saving it ?
    – Blackbelt
    Nov 8 at 13:05










  • I'm assuming, cuz i'm not getting the value
    – Tsur Yohananov
    Nov 8 at 13:06










  • you have an infinite loop (while(true)) but you are reading the value outside it
    – Blackbelt
    Nov 8 at 13:07










  • are you sure that it executes the code that should save it? Have you tried debugging?
    – Vladyslav Matviienko
    Nov 8 at 13:07










  • I am sure it executes
    – Tsur Yohananov
    Nov 8 at 13:08















up vote
1
down vote

favorite
1












for some reason SharedPreferences doesn't save the values, the code is in a service...



 Thread thread = new Thread() 
public void run()
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.not_local_sp), MODE_PRIVATE);
long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L);
int scanInterval = 500 * 60 * 60;

while (true)
if (System.currentTimeMillis() - lastExecuted >= scanInterval && contextCreator == null)
Log.d(TAG, "inside while loop");
Log.d(TAG, String.valueOf(System.currentTimeMillis() - lastExecuted));
Log.d(TAG, String.valueOf(lastExecuted));
Log.d(TAG, String.valueOf(scanInterval));
contextCreator = new ProtectionContextCreator();
feature = AppScanFactory.createAppScan(getBaseContext());
feature.add(new BoostFeature(getBaseContext(), new StubScanStore()));
contextCreator.create(getBaseContext(), feature.getType(), NotificationService.this);

/*Shared pref*/

SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(getString(R.string.timeDone), System.currentTimeMillis());
editor.commit();


try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();



;
thread.start();


the lastExecuted variable always get the default value, 0.










share|improve this question







New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • how do you know that is not saving it ?
    – Blackbelt
    Nov 8 at 13:05










  • I'm assuming, cuz i'm not getting the value
    – Tsur Yohananov
    Nov 8 at 13:06










  • you have an infinite loop (while(true)) but you are reading the value outside it
    – Blackbelt
    Nov 8 at 13:07










  • are you sure that it executes the code that should save it? Have you tried debugging?
    – Vladyslav Matviienko
    Nov 8 at 13:07










  • I am sure it executes
    – Tsur Yohananov
    Nov 8 at 13:08













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





for some reason SharedPreferences doesn't save the values, the code is in a service...



 Thread thread = new Thread() 
public void run()
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.not_local_sp), MODE_PRIVATE);
long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L);
int scanInterval = 500 * 60 * 60;

while (true)
if (System.currentTimeMillis() - lastExecuted >= scanInterval && contextCreator == null)
Log.d(TAG, "inside while loop");
Log.d(TAG, String.valueOf(System.currentTimeMillis() - lastExecuted));
Log.d(TAG, String.valueOf(lastExecuted));
Log.d(TAG, String.valueOf(scanInterval));
contextCreator = new ProtectionContextCreator();
feature = AppScanFactory.createAppScan(getBaseContext());
feature.add(new BoostFeature(getBaseContext(), new StubScanStore()));
contextCreator.create(getBaseContext(), feature.getType(), NotificationService.this);

/*Shared pref*/

SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(getString(R.string.timeDone), System.currentTimeMillis());
editor.commit();


try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();



;
thread.start();


the lastExecuted variable always get the default value, 0.










share|improve this question







New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











for some reason SharedPreferences doesn't save the values, the code is in a service...



 Thread thread = new Thread() 
public void run()
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.not_local_sp), MODE_PRIVATE);
long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L);
int scanInterval = 500 * 60 * 60;

while (true)
if (System.currentTimeMillis() - lastExecuted >= scanInterval && contextCreator == null)
Log.d(TAG, "inside while loop");
Log.d(TAG, String.valueOf(System.currentTimeMillis() - lastExecuted));
Log.d(TAG, String.valueOf(lastExecuted));
Log.d(TAG, String.valueOf(scanInterval));
contextCreator = new ProtectionContextCreator();
feature = AppScanFactory.createAppScan(getBaseContext());
feature.add(new BoostFeature(getBaseContext(), new StubScanStore()));
contextCreator.create(getBaseContext(), feature.getType(), NotificationService.this);

/*Shared pref*/

SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong(getString(R.string.timeDone), System.currentTimeMillis());
editor.commit();


try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();



;
thread.start();


the lastExecuted variable always get the default value, 0.







android multithreading save sharedpreferences






share|improve this question







New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 13:00









Tsur Yohananov

82




82




New contributor




Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Tsur Yohananov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • how do you know that is not saving it ?
    – Blackbelt
    Nov 8 at 13:05










  • I'm assuming, cuz i'm not getting the value
    – Tsur Yohananov
    Nov 8 at 13:06










  • you have an infinite loop (while(true)) but you are reading the value outside it
    – Blackbelt
    Nov 8 at 13:07










  • are you sure that it executes the code that should save it? Have you tried debugging?
    – Vladyslav Matviienko
    Nov 8 at 13:07










  • I am sure it executes
    – Tsur Yohananov
    Nov 8 at 13:08

















  • how do you know that is not saving it ?
    – Blackbelt
    Nov 8 at 13:05










  • I'm assuming, cuz i'm not getting the value
    – Tsur Yohananov
    Nov 8 at 13:06










  • you have an infinite loop (while(true)) but you are reading the value outside it
    – Blackbelt
    Nov 8 at 13:07










  • are you sure that it executes the code that should save it? Have you tried debugging?
    – Vladyslav Matviienko
    Nov 8 at 13:07










  • I am sure it executes
    – Tsur Yohananov
    Nov 8 at 13:08
















how do you know that is not saving it ?
– Blackbelt
Nov 8 at 13:05




how do you know that is not saving it ?
– Blackbelt
Nov 8 at 13:05












I'm assuming, cuz i'm not getting the value
– Tsur Yohananov
Nov 8 at 13:06




I'm assuming, cuz i'm not getting the value
– Tsur Yohananov
Nov 8 at 13:06












you have an infinite loop (while(true)) but you are reading the value outside it
– Blackbelt
Nov 8 at 13:07




you have an infinite loop (while(true)) but you are reading the value outside it
– Blackbelt
Nov 8 at 13:07












are you sure that it executes the code that should save it? Have you tried debugging?
– Vladyslav Matviienko
Nov 8 at 13:07




are you sure that it executes the code that should save it? Have you tried debugging?
– Vladyslav Matviienko
Nov 8 at 13:07












I am sure it executes
– Tsur Yohananov
Nov 8 at 13:08





I am sure it executes
– Tsur Yohananov
Nov 8 at 13:08













1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










you have an infinite loop (while(true)) but you are reading the value outside it



this line



long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L)


should be inside the while loop (if you are using lastExecuted to check if the value was stored)






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',
    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
    );



    );






    Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208285%2fsharedpreferences-doesnt-save-values%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    you have an infinite loop (while(true)) but you are reading the value outside it



    this line



    long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L)


    should be inside the while loop (if you are using lastExecuted to check if the value was stored)






    share|improve this answer
























      up vote
      0
      down vote



      accepted










      you have an infinite loop (while(true)) but you are reading the value outside it



      this line



      long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L)


      should be inside the while loop (if you are using lastExecuted to check if the value was stored)






      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        you have an infinite loop (while(true)) but you are reading the value outside it



        this line



        long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L)


        should be inside the while loop (if you are using lastExecuted to check if the value was stored)






        share|improve this answer












        you have an infinite loop (while(true)) but you are reading the value outside it



        this line



        long lastExecuted = sharedPref.getLong(getString(R.string.timeDone), 0L)


        should be inside the while loop (if you are using lastExecuted to check if the value was stored)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 13:08









        Blackbelt

        126k22207236




        126k22207236




















            Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.












            Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.











            Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208285%2fsharedpreferences-doesnt-save-values%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            PHP code is not being executed, instead code shows on the page

            Administrative divisions of China

            Cardinality of the set of algorithms