【发布时间】:2012-01-02 16:24:30
【问题描述】:
我正在制作一个 android 应用程序,但我不知道如何才能使设置屏幕仅在第一次出现。 这是应用程序的工作方式: 用户在安装后启动应用程序并显示欢迎/设置屏幕。并且一旦用户完成设置,除非用户重新安装应用程序,否则设置屏幕将永远不会再次出现。
我怎样才能做到这一点??? 请帮助并提前非常感谢!
【问题讨论】:
标签: android screen installation
我正在制作一个 android 应用程序,但我不知道如何才能使设置屏幕仅在第一次出现。 这是应用程序的工作方式: 用户在安装后启动应用程序并显示欢迎/设置屏幕。并且一旦用户完成设置,除非用户重新安装应用程序,否则设置屏幕将永远不会再次出现。
我怎样才能做到这一点??? 请帮助并提前非常感谢!
【问题讨论】:
标签: android screen installation
使用SharedPreferences测试是否是第一次启动。
注意:以下代码未经测试。
在你的 onCreate 中(或任何你想做的事情,取决于第一次开始与否),添加
// here goes standard code
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
if(pref.getBoolean("firststart", true)){
// update sharedpreference - another start wont be the first
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("firststart", false);
editor.commit(); // apply changes
// first start, show your dialog | first-run code goes here
}
// here goes standard code
【讨论】:
进行一项辅助活动。这将是您的启动器活动。它不会包含任何布局,它只会检查应用程序的首次全新运行。如果它将首先运行,则将启动设置活动,否则将启动 MainActivity。
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
//strat DataActivity beacuase its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.ths , MainActivity.class));
finish();
}
}
}
【讨论】: