PreferenceActivity 中的变量

发布于 2025-01-08 17:41:26 字数 234 浏览 0 评论 0原文

在我的 Android 应用程序中,我想制作一个反馈对话框,该对话框将在应用程序第二次启动时显示。 我该怎么做? 我可以通过 PreferenceActivity 中的变量来做到这一点吗?如果偏好活动中的a变量是由feks++编辑的;这将是下次应用程序启动时变量的结果吗?

编辑: 我没有得到任何建议的工作答案,我可以在应用程序第一次启动时在外部或内部存储上创建一个文本文件并检查该文件是否存在吗?

In my android application i want to make a feedback dialog that will show the second time the application is started.
How can i do this ?
Can i do it by variables in a PreferenceActivity. If the a variable in the preference activity is edit by feks ++; will this be the result of the variable next time the app is started ?

Edit:
I dont get any of the suggested answers to work, can i create a text file on the ext or internal store the first time the app is started and check if the file exists ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下的泡沫是彩色的 2025-01-15 17:41:26

使用共享首选项:

public class MainActivity extends Activity {
  private SharedPreferences mSharedPrefs;
  private static final String PREF_LAUNCH_COUNTER = "launch_counter";
  private int mLaunchCount = 0;
  @Override
  public void onCreate(Bundle savedState) {
    mSharedPrefs = getPreferences(Context.MODE_PRIVATE);
    if (savedState != null) {
      mLaunchCount = savedState.getInt(PREF_LAUNCH_COUNTER, 1);
    } else {
      mLaunchCount = mSharedPrefs.getInt(PREF_LAUNCH_COUNTER, 1);
      if(mLaunchCount > 1) {
        //code to handle when the app was launched after the first time.
      } else {
        //code for when the app was launched for the first time..
      }
      mSharedPrefs.edit().putInt(PREF_LAUNCH_COUNTER, mLaunchCount++);

    }

  }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
     outState.putInt(PREF_LAUNCH_COUNTER, mLaunchCount);
  }

}

Use SharedPreferences:

public class MainActivity extends Activity {
  private SharedPreferences mSharedPrefs;
  private static final String PREF_LAUNCH_COUNTER = "launch_counter";
  private int mLaunchCount = 0;
  @Override
  public void onCreate(Bundle savedState) {
    mSharedPrefs = getPreferences(Context.MODE_PRIVATE);
    if (savedState != null) {
      mLaunchCount = savedState.getInt(PREF_LAUNCH_COUNTER, 1);
    } else {
      mLaunchCount = mSharedPrefs.getInt(PREF_LAUNCH_COUNTER, 1);
      if(mLaunchCount > 1) {
        //code to handle when the app was launched after the first time.
      } else {
        //code for when the app was launched for the first time..
      }
      mSharedPrefs.edit().putInt(PREF_LAUNCH_COUNTER, mLaunchCount++);

    }

  }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
     outState.putInt(PREF_LAUNCH_COUNTER, mLaunchCount);
  }

}
最终幸福 2025-01-15 17:41:26

不,变量不会在活动重新启动后持续存在,因为整个对象都会被垃圾收集并重新创建。

您可以使用 SharedPreferences 来存储在应用程序启动之间必须保留的数据。

No, variables do not persist through activity restarts, because the entire object is garbage collected and recreated.

You can use SharedPreferences to store data that must be persisted between application launches.

樱花坊 2025-01-15 17:41:26

这有点野蛮的解决方案,但我身边没有 Eclipse 或 Android 手机。
我认为你可以做这样的事情:

protected boolean isSecondLaunchTime() {
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    int time = settings.getInt("launchTimes", 1);

    if(time==1 || time>2) return false;

    settings.edit().putString("launchTimes", ++time);
    settings.edit().commit();

    if(time==2) return true;
    else return false;
}

祝你好运!

It's kinda of a barbarian solution, but it did not have Eclipse or and Android phone close to me.
You can do something like that I think :

protected boolean isSecondLaunchTime() {
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    int time = settings.getInt("launchTimes", 1);

    if(time==1 || time>2) return false;

    settings.edit().putString("launchTimes", ++time);
    settings.edit().commit();

    if(time==2) return true;
    else return false;
}

Good luck !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文