如何保存天文钟时间并使用共享首选项在另一个活动中将其恢复 [英] How to save chronometer time and retrieve it in another activity using sharedpreferences
问题描述
我在Mainactivity.java
中有一个天文钟,我想使用SharedPreferences
保存开始时间,并让开始时间在其他活动中显示出来,但是我不知道该怎么做.我已经尝试过SharedPreferences
的人会更容易.那么,有谁知道如何节省开始时间并在另一项活动中获得价值?这是代码.
I have a chronometer in Mainactivity.java
I wanted to save the starting time using SharedPreferences
and get the starting time show it in another activity but I can't figure out how to do so .I've tried Service
which allows it running in the background but it failed as it can't pass the variable to another activity and people suggested using SharedPreferences
would be easier. So does anyone knows how to save the starting time and get the value in another activity? Here's the code.
MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity{
private Chronometer chronometer;
protected FirebaseAuth firebaseauth = FirebaseAuth.getInstance();
@Override
protected void onStart(){
super.onStart();
chronometer.setBase(SystemClock.elapsedRealtime());
final long startTime = (SystemClock.elapsedRealtime() - chronometer.getBase());
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = pref.edit();
@Override
public void onDataChange (DataSnapshot dataSnapshot){
String status = dataSnapshot.getValue(String.class);
if (status.equals ("occupied")) {
chronometer.start();
editor.putLong("time",startTime);
editor.commit();
}
else
mValueView1.setBackgroundColor(Color.parseColor("#66d983"));
}
@Override
public void onCancelled (DatabaseError databaseError){
}
});
SecondActivity.java
SecondActivity.java
@Override
protected void onStart() {
super.onStart();
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
mChildReference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.getValue(String.class);
// firebase status is occupied start chronometer
if(status.equals("occupied")) {
savedTime = pref.getLong("time",01);
chronometer.setBase(savedTime);
chronometer.getBase();
chronometer.setVisibility(View.VISIBLE);
}
if(!status.equals("occupied")){
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
chronometer.stop();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
推荐答案
如果仅在一个活动中使用sharedpreference,则可以在活动中使用getDefaultSharedPreferences()或getPreferences()方法.但是在您的情况下(如前所述) (在文档中)(将一个sharedPreference用于多个活动),您应该为sharedpreference分配一个名称.
这样,活动就知道要获取您的哪个共享首选项.只需在您的代码中更改
if you are using the sharedpreference only in one activity it's ok to use getDefaultSharedPreferences() or getPreferences() methods on your activity.but in your case(as mentioned in the documentation) which is using one sharedPreference for multiple activities, you should assign a name to the sharedpreference.
this way the activity knows to get which of your sharedpreferences. just change this in your code
SharedPreferences pref = getSharedPreferences("chornometer", Context.MODE_PRIVATE);
有关更多信息,请阅读文档
for more information read the documentation
这篇关于如何保存天文钟时间并使用共享首选项在另一个活动中将其恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!