我已经有这个问题了几天了。在我设置数据时,似乎我的共享首选项不起作用。用户从main开始,显示“oncreate”,然后进入设置页面(数据自动设置(现在)),当他们返回主Activity时,数据似乎不想过来。我正在使用joda Time库,我正在尝试计算两个日期之间的差异。
Main :( onCreate Code和onResume代码相同)
public class MainActivity extends Activity {
TextView tv;
Button set;
//CONST
final static long MILLIS_IN_DAY = 86400000;
final static long MILLIS_IN_HOUR = 3600000;
final static long MILLIS_IN_MINUTE = 60000;
long day, hour, minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewx);
set = (Button) findViewById(R.id.setting);
tv.setText("ONCREATE!");
//LOAD DATA
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
long loadedTime = prefs.getLong("quitDate", 0); //Load Settings Data
boolean test = prefs.getBoolean("test", false);
//get Comparison Data
DateTime newDate = new DateTime();
long updateTime = newDate.getMillis();
long diffInMillis = (updateTime-loadedTime);
//Calculate
minute = (diffInMillis/MILLIS_IN_MINUTE)%60;
hour = (diffInMillis/MILLIS_IN_HOUR)%24;
day = (diffInMillis/MILLIS_IN_DAY);
tv.setText(""+Long.toString(loadedTime));
if(test==true){
tv.setText("" + "\nDay|Hour|Minute: " + Long.toString(day) +":" + Long.toString(hour) + ":" + Long.toString(minute));
}
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent x = new Intent(MainActivity.this, Settings.class);
startActivity(x);
}
});
设定:
import org.joda.time.DateTime;
import android.content.SharedPreferences;
public class Settings extends Activity implements View.OnClickListener {
Button bAbout,bSettings,bFacebook;
//(5000);
final long MILLIS_IN_DAY = 86400000;
final long MILLIS_IN_HOUR = 3600000;
final long MILLIS_IN_MINUTE = 60000;
//Variables
long loadedTime;
int packs;
float cost;
long day, hour, minute, second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Settings");
setContentView(R.layout.activity_settings);
setUpButtons();
DateTime d1 = new DateTime(2013,9,11,0,0);
long today = d1.getMillis();
//SAVE DATA
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putLong("quitDate", today);
editor.putBoolean("test", true);
editor.commit();
minute = (today/MILLIS_IN_MINUTE)%60;
hour = (today/MILLIS_IN_HOUR)%24;
day = (today/MILLIS_IN_DAY);
TextView tv = (TextView) findViewById(R.id.TEMPDATE);
TextView tv2 = (TextView) findViewById(R.id.todayis);
tv2.setText("Today is: " + Long.toString(today));
}
答案 0 :(得分:1)
您正在使用每个活动专用的首选项。这意味着您有两组首选项:一组用于MainActivity,另一组用于设置。
以下是相关文档摘要:
使用该方法管理活动持久状态 getPreferences(int),允许您检索和修改一组 与活动关联的名称/值对。要使用偏好 在多个应用程序组件之间共享(活动, 接收者,服务,提供者),你可以使用底层 Context.getSharedPreferences()方法用于检索首选项对象 以特定名称存储
http://developer.android.com/reference/android/app/Activity.html#getPreferences(int)
您可以使用getSharedPreferences("MY_PREFERENCES", MODE_PRIVATE)
之类的内容,或只是调用PreferenceManager.getDefaultSharedPreferences(Context context)
来获取默认的共享偏好设置。