Monitor changes in the application

Asked

Viewed 38 times

1

I need a class that listens for any changes in the application (or external changes).

I made a class that extended from class Service, but I understand that it goes into the method onStartCommand only once.

I’m doing something wrong?

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("--> ", "onStartCommand");
      return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

1 answer

1

My suggestion is to create possible completion "states" (ok, error, etc.), or any other change that you want to monitor, that an external service can generate in execution and make the service store these values in a key of SharedPreferences according to the desired state, as it can be accessed in the Activity.

In the application you implement in your Activity a SharedPreferences.OnSharedPreferenceChangeListener and "listen" to the changes in the preference key specified above within the method onSharedPreferenceChanged() of that interface.

More details on how to implement in the official documentation: https://developer.android.com/guide/topics/data/data-storage.html#pref

https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.