码迷,mamicode.com
首页 > 其他好文 > 详细

第3章代码

时间:2015-07-11 10:29:51      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

1.

public class GlobalVariables {
    public static UserBean User;
}

 

2. 

public class GlobalVariables implements Serializable, Cloneable {
    /**
     * @Fields: serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    private static GlobalVariables instance;

    private GlobalVariables() {

    }

    public static GlobalVariables getInstance() {
        if (instance == null) {
            Object object = Utils.restoreObject(
                            AppConstants.CACHEDIR + TAG);
            if(object == null) {    //App首次启动,文件不存在则新建之
                object = new GlobalVariables();
                Utils.saveObject(
                        AppConstants.CACHEDIR + TAG, object);
            }
            
            instance = (GlobalVariables)object;
        }

        return instance;
    }
    
    public final static String TAG = "GlobalVariables";
    
    private UserBean user;

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
        Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
    }
    
    // —————以下3个方法用于序列化————————
    public GlobalVariables readResolve() 
            throws ObjectStreamException,
            CloneNotSupportedException {
        instance = (GlobalVariables) this.clone();
        return instance;
    }

    private void readObject(ObjectInputStream ois) 
            throws IOException, ClassNotFoundException {
        ois.defaultReadObject();
    }

    public Object Clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    public void reset() {
        user = null;

        Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
    }
}

 

3.

    public static final void saveObject(String path, Object saveObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(path);
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(saveObject);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

4.

    public static final Object restoreObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;
        File f = new File(path);
        if (!f.exists()) {
            return null;
        }
        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
            return object;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }    

 

5.

private void gotoLoginActivity() {
        UserBean user = new UserBean();
        user.setUserName("Jianqiang");
        user.setCountry("Beijing");
        user.setAge(32);        

        Intent intent = new Intent(LoginNew2Activity.this, 
                PersonCenterActivity.class);
        
        GlobalVariables.getInstance().setUser(user);
        
        startActivity(intent);
    }

 

6.

protected void initVariables() {
        UserBean user = GlobalVariables.getInstance().getUser(); 
        int age = user.getAge();
    }

 

7.

GlobalVariables.getInstance().reset();

 

8.

    public void reset() {
        user = null;
        Utils.saveObject(AppConstants.CACHEDIR + TAG, this);
    }

 

9.

 

10.

 

第3章代码

标签:

原文地址:http://www.cnblogs.com/Jax/p/4638108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!