我想在我的整个应用程序中拥有一个我希望具有常量(或常见)的特定重要事项(我从服务器每隔15秒提取一次)的列表。所以当我通过Intents(或任何其他方法)转向下一个活动时,我应该始终拥有该列表。在android中可能吗?如何保持我的应用程序的某些部分在整个应用程序中通用
我想要不同的解决方案,尽可能减少工作量。 请帮忙..
编辑:我想我没有让自己清楚。我不担心如何存储数据。我在问怎么才能实现只有一半屏幕变化(当我们从活动转移到活动时)而另一半保持不变(不移动)的视图。可以吗?
我想在我的整个应用程序中拥有一个我希望具有常量(或常见)的特定重要事项(我从服务器每隔15秒提取一次)的列表。所以当我通过Intents(或任何其他方法)转向下一个活动时,我应该始终拥有该列表。在android中可能吗?如何保持我的应用程序的某些部分在整个应用程序中通用
我想要不同的解决方案,尽可能减少工作量。 请帮忙..
编辑:我想我没有让自己清楚。我不担心如何存储数据。我在问怎么才能实现只有一半屏幕变化(当我们从活动转移到活动时)而另一半保持不变(不移动)的视图。可以吗?
如果数据量很大,可以使用Shared Preferences或SQLite DB存储数据。如果数据量较少,则可以使用静态变量。如果你使用静态变量,当应用中发生数据可能丢失的任何崩溃。因此,静态变量的使用不太可取。
您的应用程序类的实例总是从任何活动入店。
所有你需要做的就是创建应用程序类是这样的:
public class YourApp extends Application {
....
}
然后修改下面一行在你的应用程序的AndroidManifest.xml:
<application
android:name="your.package.YourApp"
现在您可以访问这个类无处不在:
YourApp appInstance = (YourApp)getApplication();
您可以使用SQLite
数据库来存储这些数据,然后创建单例助手来读取它。
或者您可以将您的数据以XML
或JSON
格式保存为文件,然后解析它们以读取。
或者,您可以为您的数据的一个实体创建类的容器,使之序列化和SharedPreferences
存储为ArrayList<YourDataContainer>
使用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();
你看到我的编辑了吗? –
那么在这种情况下,你不需要缓存数据。您可以使用IntentService来下载数据。请参阅此项目https://github.com/commonsguy/cwac-wakeful。有关IntentService的更多信息,请查看http://developer.android.com/reference/android/app/IntentService.html –
@ LalithB - 我对数据不感兴趣..我对视图感兴趣! –
@ Kameswari-我取数据从服务器每15秒......所以我不看好数据太多担心......但它不应该获取数据的每时间新的活动被提取! –