在Android上保存对象

时间:2014-02-15 01:19:55

标签: java android fileoutputstream objectinputstream objectoutputstream

我正在尝试保存序列化的计时器对象,然后检索它。计时器需要完全恢复它的创建时间。

计时器工作得非常漂亮,但是当应用程序被销毁时,我的计时器及其所有数据也会被破坏。

编辑:调试日志说FileNotFoundException:open failed(只读文件系统)

  • 我的清单中有使用权限
  • 我没有尝试写入SD卡,我想在本地用户的android上创建文件。 .......

我的计时器类实现了序列化

OnCreate我的应用尝试连接到objectinputstream和fileinputstream;检索对象,将其转换为Timer并分配它。

每次更新时都会存储计时器。

定时器类代码

package com.example.theworkingbutton;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import android.widget.Button;

public class Timer implements Serializable {

    private static final long serialVersionUID = 1L;

    public Timer(int timerState){
        this.timerState = timerState;

    }

    public int timerState = 0;
    public long timerStart = 0;
    public long timerEnd = 0;
    public long timeAccumulated = 0;

    public long totalSeconds = 0;
    public long hours = 0;
    public long minutes = 0;
    public long seconds = 0;

    public String realTimeSeconds = "null";
    public String realTimeMinutes = "null";
    public String realTimeHours = "null";
    public String timeString = "No time avalible";

    Button button;

    public void preform(){

        if(timerState == 0){

            timerStart =  System.nanoTime();        
            timerState = 1;

        } else if (timerState == 1) {

            timerEnd = System.nanoTime();
            timeAccumulated = timerEnd - timerStart + timeAccumulated;

            totalSeconds = TimeUnit.SECONDS.convert(timeAccumulated, TimeUnit.NANOSECONDS);
            hours = (totalSeconds / 3600);
            minutes = (totalSeconds % 3600) / 60;
            seconds = (totalSeconds % 60);

            realTimeHours = Long.toString(hours);
            realTimeSeconds = Long.toString(seconds);
            realTimeMinutes = Long.toString(minutes);
            timeString = "Hours: " + realTimeHours + " Minutes: " + realTimeMinutes + " Seconds: " + realTimeSeconds;

            timerState = 0;
        }
    }
}

结束定时器类代码

ONCREATE代码

@Override
    protected void onCreate(Bundle savedInstanceState){

        //make screen
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
            500.0f, locationListener);

       //get buttons / turn them red
        workingButton = (Button) findViewById(R.id.timer_button);
        officeButton = (Button) findViewById(R.id.office_button);
        drivingButton = (Button) findViewById(R.id.drive_button);
        showingButton = (Button) findViewById(R.id.showing_button);
        prospectingButton = (Button) findViewById(R.id.prospect_button);
        listingButton = (Button) findViewById(R.id.listing_button);

        listingButton.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
        workingButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
        drivingButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
        officeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
        showingButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
        prospectingButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);


      //set up our map
        GoogleMap monthlyMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            location.getLatitude();
            location.getLongitude();
        }
        monthlyMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        monthlyMap.setMyLocationEnabled(true);
        monthlyMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), 15 ));


        try{
        @SuppressWarnings("resource")
        ObjectInputStream is = new ObjectInputStream(new FileInputStream("TheWorkingButtonSaves.txt"));
        Timer timerOne = (Timer) is.readObject();
        workingTimer = timerOne;}
        catch (Exception e){
            e.printStackTrace();
        }
    }

结束创建代码

我如何尝试保存

 Timer workingTimer;
    public void startWorking(View view){

        if (workingTimer == null){
            workingTimer = new Timer(working);}

        //Layout Views
        workingButton = (Button) findViewById(R.id.timer_button);
        TextView amountOfTime = (TextView) findViewById(R.id.time_spent);

        if(drivingTimer.timerState == 1){
            drivingSomewhere(findViewById(R.id.drive_button));
        }

        workingTimer.preform();


        // Button Colors
         if (workingTimer.timerState == 0 ){     //(TIMERS OFF)
            workingButton = (Button) findViewById(R.id.timer_button);
            workingButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);

         } 
         else {  //(TIMERS ON)
             workingButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
         }

         //save
         try {

            FileOutputStream fs = new FileOutputStream ("TheWorkingButtonSaves.txt");
            ObjectOutputStream os = new ObjectOutputStream(fs);

            os.writeObject(workingTimer);
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
            }

         //Display Time to user
         amountOfTime.setText(workingTimer.timeString);
    }   

结束如何保存

2 个答案:

答案 0 :(得分:0)

如果有帮助,请参阅以下链接。在我看来,最好让平台处理序列化/反序列化而不是我们这样做。

Handling runtime changes

答案 1 :(得分:0)

您可以使用SharedPreferences保存计时器的值。

   SharedPreferences saveTimer= getSharedPreferences("saveTimer", Activity.MODE_PRIVATE);
   SharedPreferences.Editor editor= timeSaved.edit();
   // There's also putLong and putFloat
   editor.putInt("saveTimer", INT_NAME_1);
   editor.commit();

然后使用

检索它
    SharedPreferences getTimer = getSharedPreferences("saveTimer", Activity.MODE_PRIVATE);
    int INT_NAME_2 = getTimer.getInt("saveTimer", 0);

现在你有一个int(或float或long)值,你可以放回并从你开始的地方开始。 (您也可以使用它来检索您在其他活动中保存的任何值)