Reputation: 695
I want to make a step counter on Android with TYPE_STEP_COUNTER.As you know,
TYPE_STEP_COUNTER will reset when you re-boot the device.So I should conserve the steps.I decide to use SharedPreference to conserve it.
The project structure as follows:
In the MainActivity.java,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_step = (TextView) findViewById(R.id.main_text_step);
delayHandler = new Handler(this);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SharedPreferences.Editor editor=getSharedPreferences("DateStep",MODE_PRIVATE).edit();
}
But ,when I put data in StepService.java,it failed. //StepService.java
editor.putInt("step",StepDetector.CURRENT_STEP); editor.putString("date",current_Date);
It says can't resolve symbol editor. can you give me some advive what should I do to conserve data in StepService.java?
When I declared in service.java,it shows as follows:
Upvotes: 1
Views: 2139
Reputation: 116
I would use an interface to communicate Activity and Service: it's makes code cleaner, easier to read and test. If you have never used them, it may be a bit confusing, but take a minute to understand the code:
ServiceCallback.java
public interface ServiceCallback {
void updateStepValue(int value, String date);
}
StepService.java
public class StepService extends Service {
ServiceCallback mCallback;
public void bindCallback(ServiceCallback callback) {
mCallback = callback;
}
public void unbindCallback() {
mCallback = null;
}
// Somewhere in your code, call callback method to send data to Activity
...
mCallback.updateStepValue(5, "15-07-2017");
}
Activity.java
public class Activity implements ServiceCallback {
SharedPreferences.Editor editor;
StepService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
editor = context.getSharedPreferences("preferences_key", Context.MODE_PRIVATE).edit();
// Once your service is running and assigned to service variable
// bind activity to it to listen service callbacks
service.bindCallback(this);
}
@Override
protected void onResume() {
super.onResume();
service.unbindCallback();
}
@Override
void updateStepValue(int value, String date) {
editor.putInt("step",value);
editor.putString("date",date);
editor.apply();
}
}
Upvotes: 2
Reputation: 3869
You need to commit your changes.
editor.putInt("step",StepDetector.CURRENT_STEP);
editor.putString("date",current_Date);
editor.apply(); // <-- Save the data in the Shared Preferences.
Then in your service you can get the Shared Preferences again and get the values.
You don't need to share the editor across the two classes. In the StepService
just request the Shared Preferences again:
Context mContext = context;
// ...
mContext.getSharedPreferences("DateStep",MODE_PRIVATE).edit();
// Then get the data you want.
// In StepService
Editor mEditor;
@Override
public void onCreate() {
super.onCreate();
mEditor = getApplicationContext().getSharedPreferences("DateStep",MODE_PRIVATE).edit();
// Then get the data you want anywhere in your Service
}
Upvotes: 2
Reputation: 37404
you need to declare your editor
outside onCreate
to make it visible to other methods because currently the declaration limits it's scope to only inside onCreate
method , in short it's a Local
reference variable
SharedPreferences.Editor editor;
// ^^^^^^^^^^ declare it outside
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_step = (TextView) findViewById(R.id.main_text_step);
delayHandler = new Handler(this);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
editor=getSharedPreferences("DateStep",MODE_PRIVATE).edit();
// initialize editor
}
To use editor
in other class then declare and initialize it as shown above while using the same file name DateStep
to save data in that file.
In Service class , override
onCreate
in service
and apply the same approach
SharedPreferences.Editor editor;
// ^^^^^^^^^^ declare it outside
@Override
public void onCreate() {
super.onCreate();
editor=getSharedPreferences("DateStep",MODE_PRIVATE).edit();
// initialize editor
}
Upvotes: 2