如何在整个应用程序中保持我的应用程序的某些部分通用

时间:2013-04-02 05:07:06

标签: android footer

我想要列出某些重要的事情(我每隔15秒从服务器获取一次),我希望在整个应用程序中保持不变(或常见)。因此,当我通过Intents(或任何其他方法)移动到下一个活动时,我应该始终拥有该列表。它有可能在android ??

我想要不同的解决方案,尽可能减少工作量。 请帮助..

编辑:我想我没有说清楚。我并不担心如何存储数据。我问我怎样才能实现一个视图,其中只有一半的屏幕发生变化(当我们从活动转移到活动时),而另一半保持不变(不动)。可以吗??

5 个答案:

答案 0 :(得分:1)

您的应用程序类实例始终可以从任何活动中访问。

您需要做的就是创建这样的应用程序类:

public class YourApp extends Application {
....
}

然后修改应用AndroidManifest.xml中的以下行:

<application
    android:name="your.package.YourApp"

现在,您可以随处访问此课程:

YourApp appInstance = (YourApp)getApplication();

答案 1 :(得分:1)

使用如下所示的PreferencesManager,创建POJO以访问PreferencesManager。

// TODO: Auto-generated Javadoc
/**
 * The Class PreferenceManager.
 */
public class PreferenceManager {

    /** The Constant TAG. */
    private static final String TAG = PreferenceManager.class.getSimpleName();

    /** The default shared preferences. */
    private static SharedPreferences defaultSharedPreferences = null;

    /**
     * Inits the.
     *
     * @param context the context
     */
    public static final void init(Context context){
        defaultSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
        log("Initialize PreferenceManager!");
        UserSettings.init(context);
    }

    /**
     * Save.
     *
     * @param name the name
     * @param value the value
     */
    static final void save(String name,String value){
        if( value != null ){
            Editor edit = defaultSharedPreferences.edit();
            edit.remove(name);
            edit.putString(name, value);
            edit.commit();
        }else{
            Editor edit = defaultSharedPreferences.edit();
            edit.remove(name);
            edit.commit();
        }
    }

    /**
     * Gets the.
     *
     * @param name the name
     * @param defaultValue the default value
     * @return the string
     */
    public static final String get(String name,String defaultValue){
        return defaultSharedPreferences.getString(name, defaultValue);
    }

    /**
     * Save state.
     *
     * @param name the name
     * @param state the state
     */
    public static final void saveState(String name,Bundle state){
        if( state != null && state.size() > 0 ){
            Parcel parcel = Parcel.obtain();
            parcel.writeBundle(state);
            String encodeToString = Base64.encodeToString(parcel.marshall(), Base64.DEFAULT);
            PreferenceManager.save(name, encodeToString);
        }else{
            PreferenceManager.save(name, null);
        }
        log("Saved state "+name);
    }

    /**
     * Gets the state.
     *
     * @param name the name
     * @return the state
     */
    public static final Bundle getState(String name){
        log("Get state "+name);
        String encryptedValue = "";
        try {
            encryptedValue = PreferenceManager.get(name, "");
        } catch (NullPointerException e) {
            return new Bundle();
        }
        if( "".equals(encryptedValue) ){
            return new Bundle();
        }else{
            byte[] decode = Base64.decode(encryptedValue, Base64.DEFAULT);
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(decode, 0, decode.length);
            parcel.setDataPosition(0);
            return parcel.readBundle();
        }
    }

    /**
     * Log.
     *
     * @param msg the msg
     */
    private static final void log(String msg){
        Log.d(TAG, msg);
    }

}



/**
 * The Class Settings.
 */
public class UserSettings {

    /** The settings bundle. */
    private final Bundle settingsBundle = new Bundle(1);

        /**
     * Save.
     */
    public final void save() {
        PreferenceManager.saveState(SETTINGS_STATE_NAME, settingsBundle);
    }

    /**
     * Restore.
     */
    final public void restore() {
        settingsBundle.clear();
        Bundle state = PreferenceManager.getState(SETTINGS_STATE_NAME);
        if (state.size() == 0) {
            settingsBundle.putAll(getDefaultValuesSettings());
        } else {
            settingsBundle.putAll(state);
        }
    }

    final void reset() {
        settingsBundle.clear();
    }


    /**
     * Gets the settings.
     *
     * @return the settings
     */
    public static UserSettings getSettings() {
        return settings;
    }

    /**
     * Inits the.
     *
     * @param ctx the ctx
     */
    public static final void init(Context ctx) {
        settings.restore();
        setDeviceUniqueId(ctx, settings);
    }

}

使用示例:

public class YourApplication extends Application {
....
    onCreate(){
    ....
    PreferenceManager.init(getBaseContext());
    }                                   
}

Where you need your data to be stored and retrieved use the methods like below.
UserSettings.getSettings().setUser(responseVal);
UserSettings.getSettings().save();

String response = UserSettings.getSettings().getUser();

答案 2 :(得分:0)

如果数据量很大,您可以使用共享首选项或SQLite DB存储数据。如果数据量较少,那么可以使用静态变量。如果使用静态变量,则在应用程序中发生数据可能丢失的任何崩溃时。因此静态变量的使用不太可取。

答案 3 :(得分:0)

有很多方法可以做到这一点:

  1. 您可以存储它们并放入应用程序的SQLite数据库(数据是持久的,直到您删除应用程序或通过应用程序代码删除) 请参阅SQLite用法here
  2. 您可以在手机内存中使用缓存,直到应用运行。 请参阅此处的缓存使用情况here

答案 4 :(得分:0)

  1. 您可以使用SQLite数据库存储此数据,然后创建单例帮助程序来读取它。

  2. 或者您可以使用XMLJSON格式保存数据作为文件,然后解析它们以进行阅读。

  3. 或者您可以为数据的一个实体创建类容器,使其可序列化并存储在SharedPreferences ArrayList<YourDataContainer>