I am fairly new to android, and I'm trying to pass a value from my activity to my service. I've seen other answers on this subject but I not find what's my problem, the value does not pass to the service. In my activity I've a seekbar and I want that when I change the value of seekbar ("progress") and activate the service, the service receives the value. When I close the app the value back to zero, and should be another. In my activity I save the value with sharePreferences.
Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
//SEEKBAR
textViewSensibilidad.setText("Sensibilidad: " + progress*100/seekBar.getMax()+"%");
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textViewSensibilidad.setText("Sensibilidad: " + progress*100/seekBar.getMax()+"%");
//SAVE STATE SEEKBAR
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("progress", progress);
editor.commit();
}
});
//LOAD STATE SEEKBAR
sharedPreferences = getSharedPreferences("progress" , Context.MODE_PRIVATE);
progress = sharedPreferences.getInt("progress", progress);
seekBar.setProgress(progress);
}
public void onClick(View src) {
if(toggleButton.isChecked()){
Log.d(TAG, "onClick: starting srvice");
Intent intent = new Intent(ShakeActivity.this, MyService.class);
intent.putExtra("progress", progress);
startService(intent);
...
Service
public class MyService extends Service implements SensorEventListener {
private static final String TAG = "Servicio";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand (Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Shake to Open Whatsapp Activada", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStart");
if (intent !=null && intent.getExtras()!=null){
progress = intent.getExtras().getInt("progress");
checkeado=intent.getExtras().getBoolean("checkeado");
Toast.makeText(this, "progreso: "+progress+" checkeado: "+checkeado, Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Shake to Open Whatsapp Desactivada", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onDestroy");
//player.stop();
mSensorManager.unregisterListener(this);
}
Any suggestions? errors? I thank you in advance