私は、BroadcastReceiver から拡張されたクラスで SharedPreferences を使用するのが好きです。しかし、この方法getSharedPreferences(prefName, MODE_PRIVATE);
は認められていません。クラスで取得SharedPreferences
するにはどうすればよいですか? BroadcastReceiver
ありがとう
2 に答える
0
getSharedPreferences
のメソッドでありContext
、activity
拡張Context
するため、そのまま使用できます。
他の場所で使用する場合は、コンテキストが必要です。最も簡単な方法は、この回答で提供されています
ステップ 1 : AndroidManifest.xml にクラスを追加します
ステップ 2 : この方法でクラスを作成します
public class App extends Application{
private static Context _context;
@Override
public void onCreate() {
super.onCreate();
_context = this;
}
public static Context getContext(){
return _context;
}
}
ステップ 3: コンテキストを持つ何かが必要なときはいつでも:App.getContext()
あなたの場合App.getContext().getSharedPreferences()
于 2013-05-08T15:33:55.207 に答える
0
Context
を取得するにはが必要ですSharedPreferences
。onReceive
コンテキストを提供します
于 2013-05-08T15:03:44.167 に答える