我想在安装后第一次运行应用程序时运行代码。再也不怎么做了
以下是这里的代码是我正在尝试的方式
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);
}
}
答案 0 :(得分:2)
那么你面临的问题是什么?
String settingsTAG = "AppNameSettings";
SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean run = prefs.getBoolean("run", false);
如果您第一次更新共享首选项并在“run”中存储为true后,您的代码应该有效。
答案 1 :(得分:0)
按如下方式编辑代码,
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);
}
}
首次运行后,您需要在其他部分的sharedpreference中将run的值设置为true
。