-2

我想在安装后第一次运行应用程序时运行代码。再也不会怎么做

以下是这里的代码是我正在尝试的方式

    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);
    }


}
4

2 回答 2

2

那么你面临的问题是什么?

String settingsTAG = "AppNameSettings";
SharedPreferences prefs = getSharedPreferences(settingsTAG, 0);
boolean run = prefs.getBoolean("run", false);

如果您在第一次运行后更新了共享首选项并为“运行”存储了 true,那么您的代码应该可以工作。

于 2013-07-18T08:48:45.267 回答
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);
        }
    }

首次运行后,您需要将运行的值设置为true在其他部分的 sharedpreference 中。

于 2013-07-18T08:49:21.027 回答