0

我已经完成了一个应用程序。但是我在 sharedpreferences 活动中有一些文本,当应用程序启动时,它应该从那里拉出来并将其插入到编辑文本中,但是如果应用程序完全关闭,或者手机重新启动,它就不起作用并且默认值为改为使用。

谁能告诉我这是为什么?以及您需要什么数据。

下面是主要活动。

package com.liamwli.smsbusy;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.IntentFilter;
import android.content.SharedPreferences;

public class Sms_busyActivity extends Activity {
    IntentFilter intentFilter;
    ToggleButton endis;
    EditText message;
    Button smessage;
    SharedPreferences getPrefs;
    SharedPreferences.Editor editor;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Intent i = new Intent("com.liamwli.smsbusy.PREFS");
        // startActivity(i);
        setContentView(R.layout.main);

        endis = (ToggleButton) findViewById(R.id.enableddis);

        smessage = (Button) findViewById(R.id.savemess);

        message = (EditText) findViewById(R.id.message);

        getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        editor = getPrefs.edit();

        // ---intent to filter for SMS messages received---
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");

        Boolean state = getPrefs.getBoolean("enabled", false);

        String stext = getPrefs.getString("text", "");

        message.setText(stext);

        if (message.getText().toString().contentEquals("")){
            Toast.makeText(this, "Unable to get saved message. Please resave.", Toast.LENGTH_LONG).show();
        }

        endis.setChecked(state);

        endis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub

                Log.d("SMS Busy App", "onCheckedChanged Called");

                //Toast.makeText(Sms_busyActivity.this, "App state changed",
                        //Toast.LENGTH_LONG).show();
                if (endis.isChecked()){
                    editor.putBoolean("enabled", true);
                    editor.commit();
                }else {
                    editor.putBoolean("enabled", false);
                    editor.commit();
                }

                editor.putString("message", message.getText().toString());
                editor.commit();

            }
        });

        smessage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                editor.putString("message", message.getText().toString());
                editor.commit();
                Toast.makeText(Sms_busyActivity.this, "Message Saved", Toast.LENGTH_SHORT).show();
                Log.d("smessage", "Message saved & commited");

            }
        });

    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            Log.d("SMS Busy App", "onKeyDown Called");
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    public void onBackPressed() {
        Log.d("SMS Busy App", "onBackPressed Called");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

}
4

1 回答 1

2

问题是您Editor每次通话都使用相同的,并且commit不止一次。这只会保存整个应用程序中的最新更改。

试试这个:

            editor = getPrefs.edit();
            if (endis.isChecked()){
                editor.putBoolean("enabled", true);
            }else {
                editor.putBoolean("enabled", false);
            }
            editor.putString("message", message.getText().toString());
            editor.commit();

并以相同的方式调整您的其他commit代码。

于 2012-07-25T18:22:31.613 回答