Skip to content Skip to sidebar Skip to footer

How To Reuse Getexternalstoragestate?

How can this be written in its own class to be used over and over again? And where the comment line '//Loads the List' is, I need to be able to change that at runtime. Thnx ahea

Solution 1:

I would do this:

public static boolean performExternalStorageOperation(Runnable doIfMounted) {
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

        orderASC();// Loads the listif(doIfMounted != null) {
            doIfMounted.run();
        }
        return true;
    } else if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {
        Alerts.sdCardMissing(this);
    }
    return false;
}

You can replace the Runnable with any kind of generic Listener (I use OnClickListeners a lot for actions that aren't necessarily clicks) or write your own callback class with a common method to call, but that would be my general approach.

Solution 2:

This seems a little trivial for it's own class, but one approach is:

classStorageStateChecker  {
  staticvoidstorageState(XXX param, Listener l) {
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
        l.orderASC();// Loads the list

    } elseif (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {
        Alerts.sdCardMissing(this);
    }
  }

  publicinterfaceListener {
    publicvoidorderASC();
  }
}

Note that XXX param needs replacing with whatever this represents in the call Alerts.sdCardMissing(this); as Alerts isn't an Android SDK class, I could only guess.

To use the code, just call StorageStateChecker(param /* was 'this' */, callbackClass /* implements StorageStateChecker.Listener */);

Post a Comment for "How To Reuse Getexternalstoragestate?"