0

I just want to know how to use a boolean value returned from a method.This is the method which is returning the value:

 public boolean hasConnection() { ConnectivityManager cm = (ConnectivityManager) MCQ.this.getBaseContext().getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetwork != null && wifiNetwork.isConnectedOrConnecting()) { return true; } NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobileNetwork != null && mobileNetwork.isConnectedOrConnecting()) { return true; } NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) { return true; } return false; } 

This is the method where i want to use this value:

public void setScrollViewLayoutMarginBottom() { Resources resources = this.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); Boolean b = hasConnection(); if(b == true) { px = 90 * (metrics.densityDpi/160f); } else px = 60 * (metrics.densityDpi/160f); layoutParams.bottomMargin = (int) px; layoutParams.setMargins(0, 0, 0, (int) px); sv.setLayoutParams(layoutParams); } 

Please help me.Thanks in advance.

7
  • 1
    you are already using the value retured. are you getting any compiler errors?? Commented Sep 23, 2012 at 8:27
  • 1
    Sorry, it's not clear what you want to do, and what's not working. Commented Sep 23, 2012 at 8:27
  • well, you could get rid of the temporary Boolean by just doing if(hasConnection()), but otherwise, I don't see a problem. Commented Sep 23, 2012 at 8:29
  • 1
    Note that the returned value is of type boolean, and not Boolean. There's no reason to transform the primitive value into an object. And if (b == true) is ugly. Just use if (b). Commented Sep 23, 2012 at 8:29
  • actually i am not getting the exact value returned by the first method.Even if the first method is returning false,Boolean b remains equal to true. Commented Sep 23, 2012 at 8:30

2 Answers 2

5

You're already using the return value, although you're boxing it into a Boolean instead of just using boolean for no particular reason - and you're explicitly comparing it with true, which is unusual.

I'd probably use a conditional operator instead, actually:

int scale = hasConnection() ? 90 : 60; px = scale * (metrics.densityDpi / 160f); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes sir,this was the problem.Thanks a lot.
4

You dont' need to save the return value in a variable, just use it directly. if(booleanValue == true) will be true or false, so just remove the == true, it's obsolete

 if(hasConnection()) { px = 90 * (metrics.densityDpi/160f); } else { px = 60 * (metrics.densityDpi/160f); } 

1 Comment

Thanks a lot for solving the problem sir.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.