gpt4 book ai didi

android - 如何复用 getExternalStorageState?

转载 作者:行者123 更新时间:2023-11-29 16:23:34 24 4
gpt4 key购买 nike

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

提前收到信息。

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

修订:

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

最佳答案

我会这样做:

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 替换为任何类型的通用 Listener(我经常使用 OnClickListeners 来处理不一定是点击的操作),或者编写您自己的回调类并使用通用的调用方法,但这是我的一般方法。

关于android - 如何复用 getExternalStorageState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com