简体   繁体   English

如何使用 sharedpreferences 保存计时器时间并在另一个活动中检索它

[英]How to save chronometer time and retrieve it in another activity using 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.我在Mainactivity.java Service有一个SharedPreferences背景但它失败了,因为它无法将变量传递给另一个活动,人们建议使用SharedPreferences会更容易。 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) {

            }

        });
    }

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.如果您仅在一项活动中使用 sharedpreference ,则可以在您的活动中使用 getDefaultSharedPreferences() 或 getPreferences() 方法。但在您的情况下(如文档中所述)将一个 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有关更多信息,请阅读文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM