How to Reuse getExternalStorageState?

前端 未结 2 1246
情话喂你
情话喂你 2020-12-12 02:54

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. <

2条回答
  •  -上瘾入骨i
    2020-12-12 03:20

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

    class StorageStateChecker  {
      static void storageState(XXX param, Listener l) {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            l.orderASC();// Loads the list
    
        } else if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_UNMOUNTED)) {
            Alerts.sdCardMissing(this);
        }
      }
    
      public interface Listener {
        public void orderASC();
      }
    }
    

    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 */);

提交回复
热议问题