创建在应用程序启动时返回值的方法

时间:2012-08-15 17:41:13

标签: android integer return call oncreate

我正在开发一个应用程序,我想知道用户从一个不是onCreate()的方法启动应用程序时的用户量。我已经根据我的onCreate中的当前卷创建了一个int,但由于它无法返回任何内容,我不知道如何从那里获取我的int。在onCreate()中使用我生成的int非常重要。

如何做到这一点?

2 个答案:

答案 0 :(得分:0)

如果我很了解你,你应该使用:SharedPreferences来保存这个值。

答案 1 :(得分:0)

@pawegio,如果你想使用与用户设置相同的音量级别,请使用偏好设置。 这是如何:

写入偏好

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;