2

こんにちは、問題があります...文字列データ フォーム アクティビティをサービスに送信したいのですが、データを共有設定に保存しました.すべてが機能しており、データをサービス クラスに渡すことができます..しかし、問題は文字列を共有設定に変更してEclipseからプロジェクトを実行すると、問題は発生しませんが、デバイスまたはエミュレーターからアプリケーションを開き、文字列を変更します..その時間値はサービスで更新されませんクラス、サービスクラスでは古いデータを取得します。値を表示しているデバッグを行う場合は、デバイスで古い値を表示します。助けてください

This is the code i tried...
Activity class,From where i sent the value to to the service class using alarm manager

@Override
    protected void onResume() {
        super.onResume();
// Preference Satting
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
}

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                    myIntent = new Intent(PZAlarmTTSActivity.this, TtsService.class);

                    passedText = prefs.getString("text", "<unset>");
                    //Log.i("passed Text  :", passedText);

                    // Passing the value to the service
                    Bundle bundle = new Bundle();
                    bundle.putString("k_key", passedText);
                    myIntent.putExtras(bundle);
                    Log.i("key Data  :  ", passedText);

                    // Pending intent to launch when the alarm triggers
                    pendintIntent = PendingIntent.getService(PZAlarmTTSActivity.this, 0,
                            myIntent, 0);
                    // Sets alarm to trigger
                    alarmManager.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pendintIntent);

}


Service class, where i want to receive the string value


@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "service started", Toast.LENGTH_LONG).show();

        Bundle bundle = intent.getExtras();  
        String data = bundle.getString("k_key"); // Here data is not updating in 2nd  case
        Log.i("From service class : ", data);
    }
4

1 に答える 1

0

Docが言うように:

onStart: このメソッドは非推奨です。

代わりに onStartCommand(Intent, int, int) を実装してください。

onStartCommand : クライアントが startService(Intent) を呼び出してサービスを明示的に開始するたびにシステムによって呼び出され、提供された引数と開始要求を表す一意の整数トークンを提供します。このメソッドを直接呼び出さないでください。

Threading を使用してサービスの最新の値を取得するかstartService、SharedPreferences bez から新しい値を取得するために毎回使用してサービスを開始する必要があります。サービスが開始さonStartれると、サービスが既に実行されている場合、メソッドは呼び出されません

これは多分あなたを助ける!!!

于 2012-05-23T05:31:16.293 に答える