对于我的Android 应用程序,我写了一个由应用程序中各种活动需要的实用功能组成的类。 在这个类中, 我需要一个上下文变量( 用于与文件一起工作) 和一个偏好管理者和偏好编辑的例子。 另外, 需要一个长长的整数折叠式的当前日期作为时间戳 :
private static long today;
private static Context myContext;
private static SharedPreferences sharedPrefs;
private static Editor editor;
这是初始化这些变量的正确方法。我尝试过通过下文所示的私人建筑师这样做,但是我正在寻找错误者。
private NetworkController()
{
//Getting the Unix timestamp for today
GregorianCalendar aDate = new GregorianCalendar();
GregorianCalendar tDate = new
GregorianCalendar(aDate.get(Calendar.YEAR),aDate.get(Calendar.MONTH),
aDate.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
today = (tDate.getTimeInMillis())/1000;
//The preferences manager for reading in the preferences
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(myContext);
//The preferences editor for modifying the preferences values
editor = sharedPrefs.edit();
}
一种办法是,在使用过这种类别但我不想这样做的每一项活动中,都建立这种类别的例子。 任何其他办法都可能吗?