第一次运行Android时运行一次特定代码
我想在应用程序安装后第一次运行时运行代码。再也不知道该怎么做了 下面是代码,这是我如何尝试的第一次运行Android时运行一次特定代码,android,Android,我想在应用程序安装后第一次运行时运行代码。再也不知道该怎么做了 下面是代码,这是我如何尝试的 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SecurityPrefs.setAutoSavePattern(this, true); String settingsTAG = "AppNameSettings"; Shared
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SecurityPrefs.setAutoSavePattern(this, true);
String settingsTAG = "AppNameSettings";
SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean run = prefs.getBoolean("run", false);
if (run == false)
{
run = true;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN, null,
this, LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
else
{ Intent intent1 = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN,null,this,LockPatternActivity.class);
startActivityForResult(intent1, REQ_ENTER_PATTERN);
}
}
那么你面临的问题是什么
String settingsTAG = "AppNameSettings";
SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean run = prefs.getBoolean("run", false);
如果在第一次运行后,您更新了共享首选项并为“run”存储了true,那么您的代码应该可以工作。编辑代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SecurityPrefs.setAutoSavePattern(this, true);
String settingsTAG = "AppNameSettings";
SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean run = prefs.getBoolean("run", false);
if (run == true)
{
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN, null,
this, LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
else
{
SharedPreferences.Editor editPrefs = prefs.edit();
editPrefs.putBoolean ( "run", true );
editPrefs.commit();
Intent intent1 = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN,null,this,LockPatternActivity.class);
startActivityForResult(intent1, REQ_ENTER_PATTERN);
}
}
第一次运行后,您需要在其他部分的SharedReference中将运行值设置为
true
。布尔值保存在哪里?我认为在SharedReferencesNo中,我的意思是您必须先在SharedReferences中放入一些内容,然后才能获取它,检查Vigby或回答它是否正确。只有在第一次运行后更新“run”的值,它才会起作用。Vibgyor的代码将起作用。那样做<代码>SharedReferences.Editor editPrefs=prefs.edit();editPrefs.putBoolean(“运行”,真);commit()代码>让我来吧等一下:-p