如何重用getExternalStorageState? [英] How to Reuse getExternalStorageState?
本文介绍了如何重用getExternalStorageState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这怎么能写在自己的课堂上使用一遍又一遍?而且那里的注释行//加载列表是的,我需要能够改变在运行时。
日Thnx提前的时间信息。
/ **
* - 检查,看是否SD卡安装和放大器;加载有序列表
* ================================================= =====================
** /
私人无效storageState(){
如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_MOUNTED)){
orderASC(); //加载列表
}否则,如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_UNMOUNTED)){
Alerts.sdCardMissing(本);
}
}
修订版:
类StorageStateChecker {
静态无效storageState(活动参数,侦听器l){
如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_MOUNTED)){
l.orderASC_Label(); //将清单按标签ASC
l.orderDSC_Label();
l.orderASC_Title(); //将清单按标题ASC
l.orderDSC_Title();
}否则,如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_UNMOUNTED)){
//传递上下文AlertDialog.Builder
AlertDialog alertDialog =新AlertDialog.Builder(空).create();
alertDialog.setTitle(外部存储状态);
alertDialog.setMessage(你的SD卡没有安装如果设备插入电脑通过USB,请disconect设备!);
alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
//this.finish();
}
});
// alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
}
公共接口监听{
公共无效orderASC_Label();
公共无效orderDSC_Label();
公共无效orderASC_Title();
公共无效orderDSC_Title();
}
}
解决方案
我会做这样的:
公共静态布尔performExternalStorageOperation(Runnable的doIfMounted){
如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_MOUNTED)){
orderASC(); //加载列表
如果(doIfMounted!= NULL){
doIfMounted.run();
}
返回true;
}否则,如果(android.os.Environment.getExternalStorageState()。等于(
android.os.Environment.MEDIA_UNMOUNTED)){
Alerts.sdCardMissing(本);
}
返回false;
}
您可以替换了Runnable与任何一种通用的侦听器(我用OnClickListeners很多的不一定是点击操作)或者调用常用的方法编写自己的回调类,但是这将是我的一般方法。
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();
}
}
解决方案
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.
这篇关于如何重用getExternalStorageState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文