onCreate() ではないメソッドからユーザーがアプリを起動したときにユーザーが持っていたボリュームを知りたいアプリケーションを開発しています。onCreate 内の現在のボリュームに基づいて int を作成しましたが、何も返せないため、そこから int を取得する方法がわかりません。onCreate() で生成した int を使用することが非常に重要です。
これはどのように行うことができますか?
あなたのことをよく理解していれば、SharedPreferencesを使用してその値を保存する必要があります。
@pawegio右、ユーザーが設定したのと同じvolレベルを使用したい場合は、設定を使用してください。方法は次のとおりです。
設定に書き込む
SharedPreferences pref =
context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);
/*
* Get the editor for this object. The editor interface abstracts the implementation of
* updating the SharedPreferences object.
*/
SharedPreferences.Editor editor = pref.edit();
/*
* Write the keys and values to the Editor
*/
editor.putInt("VolumLevel", 60);
/*
* Commit the changes. Return the result of the commit.
*/
e.commit();
設定から読み取る
SharedPreferences pref = context.getSharedPreferences("MyAppPreferences", MODE_PRIVATE);
int volLevel = pref.getInt("VolumLevel", 50 /*Default if value wasn't setup yet*/);
return volLevel;