如何重用getExternalStorageState?

时间:2011-05-04 20:23:17

标签: android class sd-card

如何将它写在自己的类中以便一次又一次地使用?注释行“//加载列表”的位置是,我需要能够在运行时更改它。

Thnx提前获取信息。

/**
 * -- Check to See if the SD Card is Mounted & Loads the Ordered List
 * ======================================================================
 **/
private void storageState() {
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

        orderASC();// Loads the list

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

REVISED:

class StorageStateChecker  {
  static void storageState(Activity param, Listener l) {
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

        l.orderASC_Label();//Load the list by Label ASC
        l.orderDSC_Label();
        l.orderASC_Title();//Load the list by Title ASC
        l.orderDSC_Title();

    } else if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {

            // Pass context to AlertDialog.Builder
            AlertDialog alertDialog = new AlertDialog.Builder(null).create();
            alertDialog.setTitle("External Storage State");
            alertDialog.setMessage("Your SD-Card is not mounted!  If the device is plugged into a computer via the USB, please disconect the device.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //this.finish();
                }
            });
            // alertDialog.setIcon(R.drawable.icon);
            alertDialog.show();
    }
  }

public interface Listener {
    public void orderASC_Label();
    public void orderDSC_Label();
    public void orderASC_Title();
    public void orderDSC_Title();
  }
}

2 个答案:

答案 0 :(得分:3)

我会这样做:

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

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

您可以使用任何类型的通用侦听器替换Runnable(我使用OnClickListeners进行很多不一定是点击的操作)或使用通用方法编写自己的回调类来调用,但这将是我的一般方法。

答案 1 :(得分:1)

对于它自己的班级来说,这似乎有点微不足道,但有一种方法是:

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();
  }
}

请注意XXX param需要替换为this中代表的Alerts.sdCardMissing(this)代表的内容;由于警报不是Android SDK类,我只能猜测。

要使用该代码,只需致电StorageStateChecker(param /* was 'this' */, callbackClass /* implements StorageStateChecker.Listener */);