Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

android - 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 ahead of time for the info.

/**
 * -- 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();
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would do this:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...