Hi friends i have a problem…i want to send string data form activity to service, and i have saved the data into shared preference..mine everything is working, i able to pass data to the service class..but problem is that, if i change the string into shared preference and run my project from eclipse then i am not facing any problem, but i open the application from device or emulator..and then change my string..that time value is not updating in service class, in service class its taking the old data.
I if debug its showing the value, but in device showing old value. Any help please
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);
}
see As Doc says:
onStart:This method is deprecated.
Implement onStartCommand(Intent, int, int) instead.
onStartCommand :Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.
you need to use Threading to get latest value in your service or start your service by using
startServiceevery time for getting new value from SharedPreferences bez once service is started youronStartmethod is not called if service is already runningthis maybe help you!!!