Java 从片段中的活动获取布尔值

Java 从片段中的活动获取布尔值,java,android,android-fragments,Java,Android,Android Fragments,奇怪的是这个问题还没被问出来 我需要检查活动中的电话存储,并在片段中获取返回值 /*MainActivity*/ boolean hasLowStorage(){ IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); return registerReceiver(null, lowStorageFilter) != null; } 从活动调用void方法时,我们执

奇怪的是这个问题还没被问出来

我需要检查活动中的电话存储,并在片段中获取返回值

/*MainActivity*/
boolean hasLowStorage(){
    IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
    return registerReceiver(null, lowStorageFilter) != null;
}
从活动调用void方法时,我们执行以下操作:

 /*fragment*/
((MainActivity) getActivity()).yourMethod();
如何从片段中调用布尔值?

尝试以下操作:

boolean yourValue = ((MainActivity) getActivity()).hasLowStorage();
试试这个:

boolean yourValue = ((MainActivity) getActivity()).hasLowStorage();

将方法的范围更改为public,然后重试

//主要活动

public boolean hasLowStorage(){
        IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        return registerReceiver(null, lowStorageFilter) != null;
    }
//碎片

    boolean bolValue = ((MainActivity) getActivity()).hasLowStorage();

将方法的范围更改为public,然后重试

//主要活动

public boolean hasLowStorage(){
        IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        return registerReceiver(null, lowStorageFilter) != null;
    }
//碎片

    boolean bolValue = ((MainActivity) getActivity()).hasLowStorage();

像这样公开

 /*MainActivity*/
 public static boolean hasLowStorage(){
 IntentFilter lowStorageFilter = new 
 IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
 return registerReceiver(null, lowStorageFilter) != null;
 }
就这样说吧

 boolean yourValue = 
 MainActivity.hasLowStorage();

快乐编码:)

像这样公开它

 /*MainActivity*/
 public static boolean hasLowStorage(){
 IntentFilter lowStorageFilter = new 
 IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
 return registerReceiver(null, lowStorageFilter) != null;
 }
就这样说吧

 boolean yourValue = 
 MainActivity.hasLowStorage();
快乐编码:)