私の Android アプリケーションでは、アプリケーションのさまざまなアクティビティで必要なユーティリティ関数で構成されるクラスを作成しました。このクラスでは、(ファイルを操作するための) コンテキスト変数と、設定マネージャーと設定エディターのインスタンスが必要です。また、タイムスタンプとして現在の日付を表す long integer が必要です。
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();
}
1 つのアプローチは、使用されるすべてのアクティビティでこのクラスのインスタンスを作成することですが、私はそれをしたくありません。他のアプローチは可能ですか?