I have a service that tracks battery information and stores it in Shared Preferences. Then from my main activity I have BroadcastReceiver(that has OnReceive for listening ACTION_BATTERY_CHANGED)
Every time OnReceive occurs battery level and temperature changes, but my Shared Preferences leaves the same until I change my screen orientation(I think because of recreation of activity).
Please, help me to somehow make onReceive method also update shared preferences.
My Main Activity:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
appPrefs = new AppPreferences(getApplicationContext());
}
......
/* Broadcast Receiver for battery stats */
BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
.....
private TextView text;
int level = -1;
#Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
text=(TextView) findViewById(R.id.TV_text);
text.setText("level"+level+"something from Shared Pref"+appPrefs.getAnything());
}
}
}
My Shared Preferences:
public class AppPreferences extends PreferenceActivity {
private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; // Name of the file -.xml
private SharedPreferences appSharedPrefs;
private Editor prefsEditor;
public AppPreferences(Context context)
{
this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
this.prefsEditor = appSharedPrefs.edit();
}
public long getAnything() {
return appSharedPrefs.getLong("Anything", 9);
}
public void setAnything(long a) {
prefsEditor.putLong("Anything", a);
prefsEditor.commit();
}
My service:
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
......
appPrefs.setAnything(a);
So, why Shared Preferences are not updated as level or temperature in OnReceive method?
And what to do? Any help will be appreciated. :)
Finally, after so much research, I could make it work: just add this:
private Intent starterIntent;
in your class members area above class name, in my example below main class:
public class MainActivity extends Activity {
private Intent starterIntent;
Then anywhere you want place a code to refresh activity without showing to user.:
finish();
starterIntent.setFlags(intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(starterIntent);
overridePendingTransition(0,0);
Related
I am an android beginner and tried almost every method to use Shared Preferences but when I try to get value in nonactivity class method doesn't work ONLY IF APP REMOVED FROM RECENT APPS.
My Scenario is like:
Class LocationRequestHelper.java
import android.content.Context;
import android.preference.PreferenceManager;
public class LocationRequestHelper {
...
...
public static String getUserId() {
return MainActivity.preferences.getString("userId","");
}
public static void setUserId(String userId) {
MainActivity.preferences.edit().putString("userId", userId ).commit();
}
}
MainActivity
public class MainActivity extends FragmentActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
public static SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
LocationRequestHelper.setUserId("1");
....
....
Utils
public class Utils {
public static void getLocationUpdates(final Context context, final Intent intent, String broadcastevent){
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Location firstLocation = locations.get(0);
accuracy = firstLocation.getAccuracy();
LocationData data = new LocationData();
// MY FUNCTION DOESN'T WORK HERE
data.setUsrid(LocationRequestHelper.getUserId());
updateServer(data);
}
}
getLocationUpdates is called from BroadcastReceiver
Now problems appears after I try to get userID using
LocationRequestHelper.getUserId();
EDITED : Added Broadcast code
LocationUpdatesBroadcastReceiver
public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "LUBroadcastReceiver";
public static final String ACTION_PROCESS_UPDATES ="PROCESS_UPDATES";
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
Utils.getLocationUpdates(context,intent,"PROCESS_UPDATES");
}
}
}
}
NOTE:: THIS WORKS FINE IF APP IS IN BACKGROUND. BUT CRASHES AFTER I KILL FROM RECENT APPS.
Please tell me how I can send saved user id in my api call?
sorry for caps but this is main problem :P
The reason the app is crashing is that when it is killed any static variables become null. However you are still trying to access the SharedPreferences from the broadcast receiver and as such are getting a NPE.
The first thing you can do is update your LocationRequestHelper class to accept a context in the method rather than using the static SharedPreferences:
public class LocationRequestHelper {
//...
public static String getUserId(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString("userId","");
}
public static void setUserId(Context context, String userId) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("userId", userId ).commit();
}
}
Then update you MainActivity to get rid of the SharedPrefs business and change the call to:
LocationRequestHelper.setUserId(this, "1");
And the Utils class to:
data.setUsrid(context, LocationRequestHelper.getUserId());
I'm trying to take string from user and use it later when the app is closed..now it works just when the app is in background but i lose the string when i close the app..is there a way to do it like this or i have to use SharedPreference and if i have to use it please explain how because i tried and failed..thanks alot.
this is my code in my MainActivity to the string from the EditText
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private static String reminder;
private EditText et;
private Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize variables
sharedPreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et = (EditText) findViewById(R.id.Name);
reminder = et.getText().toString();
if(reminder == null){
reminder = "TWEAK!";
}
editor.putString("TAG",reminder);
editor.commit();
// do stuff
}
// get the user's string
public String getRem() {
reminder = sharedPreferences.getString("TAG", "");
return reminder;
}
the app crashes and gives
"Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference"
at this line
reminder = sharedPreferences.getString("TAG", "");
this is the class where i call the method
public class Notifications extends BroadcastReceiver {
private String rem;
// set notification
#Override
public void onReceive(Context context, Intent intent) {
// object to access MainActivity methods
MainActivity main = new MainActivity();
rem = main.getRem();
}
Wherever you called the method of getRem(), you can't do that outside the Activity as the SharedPreferences are null.
Like, I assume you made a new MainActivity(), then called getRem() on that, perhaps?
You need to obtain the SharedPreferences again from an available Context, and then you can use getString("TAG", "")
EDIT Borrowed from Shared preferences inside broadcastreceiver
public class Notifications extends BroadcastReceiver {
private String rem;
// set notification
#Override
public void onReceive(Context context, Intent intent) {
setRem(context);
}
private void setRem(Context context) {
SharedPreferences prefs = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
rem = prefs.getString("TAG", "");
}
}
You can use sharedpreferences
SharedPreferences sharedpreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("TAG",reminder);
editor.commit();
to retrive it:
sharedpreferences.getString("TAG","");
[update]
public class Notifications extends BroadcastReceiver {
private String rem;
// set notification
#Override
public void onReceive(Context context, Intent intent) {
// object to access MainActivity methods
SharedPreferences sharedPreferences = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
rem = sharedPreferences.getString("TAG", "");
}
more about SharedPreferences
I created Service class.
I can run it anywhere where I want, but I always need Context from MainMenuActivity.class.
I tried use getApplicationContext and getBaseContext but they show another class.
Thanks for answer
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, MyService.class));
}
}
public class MyService extends Service {
private Handler handler = new Handler();
private MyLocationListener mylistener;
public void onCreate() {
handler.postDelayed(new runnable(), 10000);
}
private class runnable implements Runnable {
#Override
public void run() {
mylistener = new MyLocationListener();
}
}
}
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
}
}
[EDIT]
When I used getApplicationContext() or getBaseContext or MainActivity.this to getDefaultSharedPreferences, always it will be the same?
Solution: 1
In that case you have to use, defaultSharedPreferences. You can access the default shared preferences instance by:
PreferenceManager.getDefaultSharedPreferences(Context context):
Example:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
This preference is shared across all your Activity and Service classes.
Solution: 2
You can create sharedPreference instance in your application class like:
public class MyApplication extends Application {
public static SharedPreferences preferences;
#Override
public void onCreate() {
super.onCreate();
preferences = getSharedPreferences("Preferences", MODE_PRIVATE);
}
}
And then you can manage your preferences as:
MyApplication.preferences.getString("key", "default");
add context parameter to your service class methods
public void myMethodInsideServiceClass(Context context){
//bluh bluh
}
so that you can call from Activity Class like this
myMethodInsideServiceClass(this);
you can also try
public class MyActivity extends Activity {
public static myActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myActivity=this;
}
}
so that you can use myActivity across the application
(i was not using editor to type code, sorry for syntax error)
When using a thread/task within an android service that implements the OnSharedPreferenceChangeListener interface, the changes made in the preference screen aren't reflected back to the thread/task object within the android service.
I want to accomplish two things:
SharedPreference data should be loaded when MyTask is constructed and initialized.
When preference change occurs, MyTask object must be updated with the new preference values set in the preference screen.
The problem is: preference initialization and preference changes are not reflected to the MyTask object.
This is my setup (only essential parts are mentioned):
MyService.class:
public class MyService extends Sevice {
private MyTask myTask;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!serviceStarted) {
serviceStarted = true;
myTask = new MyTask(this);
Thread t = new Thread(myTask);
t.start();
}
return Service.START_STICKY;
}
#Override
public void onDestroy() {
myTask.cancel();
super.onDestroy();
}
}
MyTask.class:
public MyTask implements Runnable, OnSharedPreferenceChangeListener {
private Context mContext;
private boolean mCancelled;
public MyTask(Context context) {
mContext = context;
}
#Override
public void run() {
while(!mCancelled) {
// do something
}
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// FIXME: DOESN'T GET CALLED after change in preference!!!!
Log.d(TAG, "Key= " + key);
}
public void cancel() {
mCancelled = true;
}
}
preference_devices.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="pref_category_devices"
android:title="#string/pref_category_devices_title" >
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_devices_server"
android:title="#string/pref_devices_server_title" />
</PreferenceCategory>
</PreferenceScreen>
I have tried coding a SharedPreferences listener object as a member field of the MyTask class and register/unregister the listener from the provided context, but that didn't work either. These changes also didn't work:
MyTask.class (using SharedPreference listener as field member of class):
public MyTask implements Runnable {
private Context mContext;
private boolean mCancelled;
private boolean mServerEnabled;
private SharedPreferences mPrefs;
private SharedPreferences.OnSharedPreferenceChangeListener
mPreferenceListener;
public MyTask(Context context) {
mContext = context;
mPrefs = mContext.getSharedPreferences("pref_category_devices",
Context.MODE_PRIVATE);
mPreferenceListener = new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
// FIXME: DOESN'T GET CALLED after change in preference!!!!
Log.d(TAG, "Key= " + key);
}
};
mPrefs.registerOnSharedPreferenceChangeListener(mPreferenceListener);
// set the initial value of the preference setting
mServerEnabled = mPrefs.getBoolean("pref_devices_server", false);
}
#Override
public void run() {
while(!mCancelled) {
// do something
}
}
public void cancel() {
mCancelled = true;
}
}
I have now reached the point of throwing my computer out of the window :(
Any help in the right direction is highly appreciated :)
EDIT: In the code
mPrefs = mContext.getSharedPreferences("pref_category_devices", Context.MODE_PRIVATE);
I assumed that the first argument should be the preference category name of the preference file, like: "pref_category_devices". THIS IS INCORRECT! The first argument must be a shared preference file name. That didn't solve the problem, but at least now you know to not fall for this pitfall.
=== SOLUTION: === See answer of Mr_and_Mrs_D + code below this line:
Change in MyTask:
mPrefs = mContext.getSharedPreferences("pref_category_devices",
Context.MODE_PRIVATE);
into:
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
mPreferenceListener = new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("preference_name_here")) {
mPrefValue = sharedPreferences.getBoolean(key, false);
// do something with boolean pref value
}
}
};
mPrefs.registerOnSharedPreferenceChangeListener(myPreferenceListener);
Where mPrefValue is a field member of type boolean in MyTask that needs to be set when the "preference_name_here" preference changes.
Change :
private volatile boolean mCancelled; //otherwise the myTask thread may never stop
For your problem :
if (!serviceStarted) {
serviceStarted = true;
myTask = new MyTask(this);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.registerOnSharedPreferenceChangeListener(myTask); //err, you must register
Thread t = new Thread(myTask); t.start();
}
Docs :
These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this activity.
[emphasis mine]
Edit : your second snippet probably fails cause you get the wrong shared prefs - you must get the default ones - I thought it was failing because of :
SharedPreferences.onSharedPreferenceChangeListener not being called consistently
I have activity and service, I would like to get a reference to Service integer, that is being updated from time to time in Service. My problem is that in my Activity I only get that integer first declared Value (for instance 0).
My main goal is to know Services' updated value every time I start my program.
Main activity:
if(Service.doesCounter>0){
//do something
//in this state Service.doesCounter always is 0(checked by log)
}
Service:
public static int doesCounter=0; // declared after class as class memeber
//code where I start my method does();
.....
public void does(){
doesCounter++;
Log.e("cccccc","Service Counter "+doesCounter); // everything ok, value is changing as suppose to.
}
Edit
my Shared Preferences class:
public class AppPreferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; // Name of the file -.xml
private SharedPreferences appSharedPrefs;
private Editor prefsEditor;
public AppPreferences(Context context)
{
this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
this.prefsEditor = appSharedPrefs.edit();
}
public boolean getAnything() {
return appSharedPrefs.getBoolean("Anything", false);
}
public void setAnything(Boolean text) {
prefsEditor.putBoolean("Anything", text);
prefsEditor.commit();
}
Then from Main Activity:
public class MainActivity extends Activity {
protected AppPreferences appPrefs;
appPrefs = new AppPreferences(getApplicationContext());
appPrefs.setAnything(fasle);
Then from Service:
appPrefs = new AppPreferences(getApplicationContext());
And when this happens all earlier made changes are reseted, how to make service and MainActivity use same prefs? Maybe I can somehow make AppPrefs class static?
Using static class fields is considered a bad practice in android.
Your app's resources may be revoked by the os and another process of your app may be re-initialized whenever the user gets back to it. In this case you will loose doesCounter updates. I don't know if this is the case (it should work in a common scenario where your app is foregrounded, unless you are running your service in another process (using the flag isolatedProcess) .
The easiest way to achieve what you are trying to do "the android way" is to store the doesCounter in SharedPreferences.
One way to achieve that is having a static class like this:
public class PrefUtils {
private final static String NUM_DOES = "NumDoes";
public static int getNumDoes(Context c)
{
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = c.getSharedPreferences(PREF_NAME, mode);
return mySharedPreferences.getInt(NUM_DOES, 0);
}
public static void setNumDoes(int numDoes , Context c)
{
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = c.getSharedPreferences(PREF_NAME, mode);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putInt(NUM_DOES, numDoes);
editor.commit();
}
And you are done. Just call PrefUtils.getNumDoes / setNumDoes