简体   繁体   English

Android应用不会从共享偏好活动中提取信息。 使用默认值

[英]Android app not pulling info from sharedpreferences activity. Using default

I have finished making an app. 我已经完成了一个应用程序。 But I have some text in a sharedpreferences activity, and when the app starts it should pull it from there and insert it into an edittext, but if the app is fully closed, or the phone rebooted it doesn't work and the default value is used instead. 但是我在共享首选项活动中有一些文本,当应用程序启动时,它应该从那里拉出并将其插入到edittext中,但如果应用程序完全关闭,或者手机重新启动它不起作用,默认值为用来代替。

Could anyone tell me why this is? 谁能告诉我为什么会这样? And what data you would need. 你需要什么数据。

Below is the main activity. 以下是主要活动。

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

}

The problem is that you are using the same Editor for every call, and commit ting it more than once. 问题是你为每个调用使用相同的Editor ,并且不止一次地commit它。 This will only save the most recent change throughout the entire application. 这只会保存整个应用程序中的最新更改。

Try this: 尝试这个:

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

And adjust your other commit code in the same manner. 并以相同的方式调整您的其他commit代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将JSON数组保存到SharedPreferences中并在其他活动中再次读取。 Android的 - How to save a JSON array into SharedPreferences and read again in a different activity. Android 如果从最近的活动android中删除应用程序,则SharedPreferences被清除 - SharedPreferences cleared if remove the app from recent activity android 使用选项卡活动的Android SharedPreferences - Android SharedPreferences using tab activity Android SharedPreferences从片段到活动 - Android SharedPreferences from Fragment to Activity Android:从活动中获取有关调用应用程序的信息 - Android: getting info on the calling app from activity 将信息从编辑文本拉到另一个活动? - Pulling info from edit text to another activity? Android:无法从 Activity 更新 Fragment 中的文本视图。 空指针异常 - Android: Can't update textview in Fragment from Activity. NullPointerException 使用GSON将对象保存在SharedPreferences中并从Android Studio中的活动中读取 - Saving an Object in SharedPreferences and Reading it From an Activity in Android Studio Using GSON 如何防止Android杀死我的活动。 服务也许? - How to Prevent Android From Killing My Activity. Services Maybe? 调用片段活动错误无法实例化活动。 不能转换为 android.app.Activity - Call Fragment Activity error Unable to instantiate activity. cannot be cast to android.app.Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM