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、後でそれを指定SharedPreferenceするKeyと、保存された値が吐き出されValueます。

したがって、私のアプリケーションでは、設定 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 に答える