SharedPreferences doesn't save values
up vote
1
down vote
favorite
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
New contributor
add a comment |
up vote
1
down vote
favorite
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
New contributor
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
New contributor
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
android multithreading save sharedpreferences
New contributor
New contributor
New contributor
asked Nov 8 at 13:00
Tsur Yohananov
82
82
New contributor
New contributor
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 8 at 13:08
Blackbelt
126k22207236
126k22207236
add a comment |
add a comment |
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.
Tsur Yohananov is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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