0

我有问题的部分如下:

public class MainActivity extends BaseActivity implements
    OnContactsInteractionListener, OnAdModeListener {

private SharedPreferences sp;
private SharedPreferences.Editor ed;

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sp = this.getSharedPreferences("me.name",
            MODE_PRIVATE);
    ed = sp.edit();



private boolean parseIntent(Intent intent) {
    if (intent.getAction().equals("SHOW_PHONE_CALL_LIST")
            && sp.getBoolean("SHOW_SECOND", false)) {
        try {
            goToPage(1);
            int random = sp.getInt("RANDOM", 9999);
            counterAdsAndLoad("2", random);
            //ed is editor object
            ed.putString("MP3PATH", "");
            ed.putBoolean("SHOW_SECOND", false);
            ed.commit()
            return true;


        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    ed.remove("RANDOM").commit();
    return false;

}

由于不合理的不良行为,我进行了调试,我看到了以下内容: if 条件为真,正如我所料,第二秒后commit()程序跳转到return false.

可能是什么原因?

4

2 回答 2

0

确保你已经遵循这个。

创建一个 SharedPrefrences 参考。

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

创建 SharedPreferncesEditor 的引用

SharedPreferences.Editor editor = prefs.edit();

将值放入编辑器

editor.putBoolean("SHOW_SECOND", true); 
editor.putString("MP3_PATH", "");

最后提交编辑

editor.commit();
于 2013-10-23T09:44:05.253 回答
0

试试这个...

public class MainActivity extends BaseActivity implements
OnContactsInteractionListener, OnAdModeListener {

private SharedPreferences sp;

...

 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  sp = getSharedPreferences("me.name",
        MODE_PRIVATE);
}

private boolean parseIntent(Intent intent) {
if (intent.getAction().equals("SHOW_PHONE_CALL_LIST")
        && sp.getBoolean("SHOW_SECOND", false)) {
    try {
        goToPage(1);
        int random = sp.getInt("RANDOM", 9999);
        counterAdsAndLoad("2", random);
        //ed is editor object
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("MP3PATH", "");
        ed.putBoolean("SHOW_SECOND", false);
        ed.commit()
        return true;


    } catch (Exception e) {
        e.printStackTrace();

    }
 }
 SharedPreferences.Editor ed = sp.edit();
 ed.remove("RANDOM").commit();
 return false;

}

于 2013-10-23T13:38:20.680 回答