Android: First run popup dialog(Android:首次运行弹出对话框)
问题描述
我正在尝试制作一个仅在应用首次运行后显示的弹出对话框,它会提醒用户应用中的新更改.所以我有一个这样的对话框弹出:
I am trying to make a popup dialog that only shows after the app's first run that will alert users of the new changes in the app. So I have a dialog popup like this:
new AlertDialog.Builder(this).setTitle("First Run").setMessage("This only pops up once").setNeutralButton("OK", null).show();
一旦他们关闭它,它就不会回来,直到下一次更新或他们重新安装应用程序.
Once they dismiss it, it won't come back until the next update or they reinstall the app.
如何将上面的对话框代码设置为只运行一次?
How can I set the dialog code above to run only once?
推荐答案
使用 SharedPreferences 来存储 isFirstRun 值,并根据该值签入您的启动活动.
Use SharedPreferences to store the isFirstRun value, and check in your launching activity against that value.
如果设置了值,则不需要显示对话框.否则,显示对话框并将 isFirstRun 标志保存在 SharedPreferences 中.
If the value is set, then no need to display the dialog. If else, display the dialog and save the isFirstRun flag in SharedPreferences.
例子:
public void checkFirstRun() {
boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
// Place your dialog code here to display the dialog
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("isFirstRun", false)
.apply();
}
}
这篇关于Android:首次运行弹出对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:首次运行弹出对话框
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
