0

我正在制作一个从时间倒计时(例如 1 分钟)然后向某人发送短信的应用程序。我正在使用首选项,以便用户可以设置时间、电话号码和消息,但我不知道如何使计时器、号码和消息变量是首选项中设置的变量,而不是默认变量。这是我到目前为止的代码。

package com.countdowntimer;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

        public class CountdownTimer extends Activity implements OnClickListener
            {
                private MalibuCountDownTimer countDownTimer;
                private long timeElapsed;
                private boolean timerHasStarted = false;
                private Button startB;
                private TextView text;
                private TextView timeElapsedView;

                private final long startTime =     30000 ; //I want this to be the value from the preferences
                private final long interval =    1000 ;

                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState)
                    {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_countdown_timer);


                        startB = (Button) this.findViewById(R.id.button);
                        startB.setOnClickListener(this);

                        text = (TextView) this.findViewById(R.id.timer);
                        timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
                        countDownTimer = new MalibuCountDownTimer(startTime, interval);
                        text.setText(text.getText() + String.valueOf(startTime/1000));
                    }

                public void onClick(View v)
                    {
                        if (!timerHasStarted)
                            {
                                countDownTimer.start();
                                timerHasStarted = true;
                                startB.setText("Start Timer");
                            }
                        else
                            {
                                countDownTimer.cancel();
                                timerHasStarted = false;
                                startB.setText("Stop Timer");
                            }
                    }

                // CountDownTimer class
                public class MalibuCountDownTimer extends CountDownTimer
                    {

                        public MalibuCountDownTimer(long startTime, long interval)
                            {
                                super(startTime, interval);
                            }

                        @Override
                        public void onFinish()
                            {
                                text.setText("Time's up!");
                            timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime/1000));
                            sendSMS("07772417392", "The timer has finished!"); //These should also be the values from the preferences
                            }

                        @Override
                        public void onTick(long millisUntilFinished)
                            {
                                text.setText("Time remaining:" + millisUntilFinished/1000);
                                timeElapsed = startTime - millisUntilFinished;
                                timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed/1000));
                            }
                }

                private void sendSMS(String phoneNumber, String Message) {

                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(phoneNumber, null, Message, null, null);
                }
        }

我已经研究过使用 SharedPreferences 和 PreferenceManager 等,但我看到的所有示例并没有真正帮助我。

编辑:添加了一些额外的代码(见下文),它没有错误,但每当我尝试启动计时器时,应用程序强制关闭,我不知道为什么。

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                 String timerLength = prefs.getString("timerLength","");
                 TextView timer = (TextView) this.findViewById(R.id.showTimer);
                 timer.setText(timerLength);
4

1 回答 1

3

Preferences,你有Key/Value关系。你设置了Key你想要存储的值,然后你给SharedPreferenceKey,它会为你吐出存储Value的值。

所以,在我的 Preference XML 的应用程序部分看起来像:

<EditTextPreference
            android:key="username"
            android:summary="@string/usernameSummary"
            android:title="@string/username" />

稍后我可以通过执行以下操作来访问用户输入的值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String username = preferences.getString("username", ""); //"" is the default String to return if the preference isn't found

所以我使用Key“用户名”来获取Value用户存储的。

于 2012-07-23T18:31:14.897 回答