0

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

Pablo Villar
  • 177
  • 1
  • 15
  • possible duplicate of [Example: Communication between Activity and Service using Messaging](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) – Simon Jan 18 '15 at 20:24

2 Answers2

0

Since you already store the progress value as a shared preference, your service can just get the shared preference value. There is no need to pass it as a value to the service.

cybersam
  • 63,203
  • 6
  • 53
  • 76
0

In your service register a shared preference listener. Docs here:

http://developer.android.com/reference/android/content/SharedPreferences.html#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)

This will allow you to make the service completely independent of the activity yet observe the value changes when they are made.

Example - make your service implement SharedPreferences.OnSharedPreferenceChangeListener interface. Then define the following method in your service:

@Override 
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  if ("progress".equals(key) {
    int progress = sharedPreferences.getInt("progress", 0);
    // we have a new value -> react here
  }
}

Finally don't forget to register the listener in the onStartCommand method:

SharedPreferences sharedPrefs = getSharedPreferences("progress", Context.MODE_PRIVATE);
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Thx. for now does not work me, but then I'm better analyze and respond again. – Pablo Villar Jan 18 '15 at 21:14
  • The problem is when I close the application. the service works but progress is zero (I verified with toast). There's something I'm doing wrong, but I know it is. – Pablo Villar Jan 20 '15 at 00:35