Office Hours — Today, March 12

Thursday, March 7

Mar 12
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
Gabriele
has entered the room
Mark M.
hello, Gabriele!
Gabriele
hi
Mark M.
how can I help you today?
7:30 PM
Kevin M.
has entered the room
Gabriele
I've added a receiver to android.net.wifi.supplicant.CONNECTION_CHANGE
Mark M.
(BTW, hello, Kevin -- be with you shortly!)
Gabriele
anyway when I receive the intent and I create a new service
the wifi can't still resolve the hostname
why?
Mark M.
I have no idea
are you saying that hostnames resolve later, but not right at the CONNECTION_CHANGE time?
Gabriele
View paste
I'm checking if I'm connected with
if (!action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
			return;

		boolean isConnected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
yes, it seems that I'm still not connected when it says I'm connected
Mark M.
I have not used CONNECTION_CHANGE
it is possible that "it is now possible to perform Wi-Fi operations" is referring to lower level things on WifiManager
and not actual TCP/IP operations
but that is just a guess
Gabriele
ah, how can I do then? I should "active waiting" for something?
Mark M.
have you considered using ConnectivityManager's broadcast instead?
7:35 PM
Gabriele
If I use it I'm sure I can send the request?
if Yes I will
Mark M.
I see more people using that than WifiManager directly for ordinary Internet work
Gabriele
ok, thank you
Mark M.
let me take a question from Kevin, and I will be back with you shortly
Kevin: do you have a question?
Gabriele
sure
Kevin M.
Yes. I'm writing a new App using Fragments and need a way to talk to the main activity
In ready your book about ViewPager, that looks like a good start
reading
Mark M.
what do you mean by "talk to the main activity"?
what is "the main activity" with respect to your "App using Fragments"?
Kevin M.
Just not sure how to handle changing the the actionbar title & menus when you go back
Mark M.
the action bar items should change automatically as the fragments come and go
the title, though, I haven't messed with
Kevin M.
I have a SherlockFragmentActivity which now using a transaction (I'm having problems with using add/replace)
When I go back, I pop the fragment stack
7:40 PM
Mark M.
I guess I do not understand what your question is
Kevin M.
It's pretty basic. I have 1 activity that will keep going forward through different screens but be able to go back. I want to make sure the title & menu are updated when they go back
Mark M.
if you are using FragmentTransactions to move to the different screens, use addToBackStack(), and the BACK button processing should be automatica
er, automatic
with respect to the title, you can try perhaps overriding onAttach() and onDetach() and use those to trigger changes to the title, though I have never tried this
Kevin M.
I'm using both but when I click on the back arrow in the actionbar, I handle the pop myself
Mark M.
I would recommend using addToBackStack(), unless you have strong reasons to do otherwise
Kevin M.
View paste
I do use that, then I use 			supportFragmentManager.popBackStack();
Mark M.
with respect to the title, the only automatic title changing that I see would be if you are using FragmentBreadCrumbs
you should not need to do both addToBackStack() and popBackStack()
Kevin M.
Not familiar with that
View paste
		FragmentTransaction ft = supportFragmentManager.beginTransaction();
//		ft.add(R.id.content, fragment, stackName);
		ft.replace(R.id.content, fragment, stackName);
		ft.addToBackStack(stackName);
		ft.commit();
Mark M.
that should be fine, even without your own popBackStack()
Kevin M.
then when they hit the back button or the back arrow, I use popBackStack
7:45 PM
Mark M.
again, if the user presses the BACK button, the last FragmentTransaction called with addToBackStack() should be automatically popped
without you calling popBackStack()
Kevin M.
Yes, FragmentActivity handles that
Mark M.
again, I do not really know what your question is
Kevin M.
I need a good communication model between the activity and each fragment
Mark M.
um, just call methods
the activity knows what the current fragment is, as it executed the FragmentTransaction that set it up
so the activity can just call methods on that fragment
the fragment can use getActivity() to retrieve the hosting activity and cast that to the activity's class (or an interface) and call methods on it
or, you are welcome to use more of a message bus approach, such as LocalBroadcastManager or Otto
Kevin M.
Part of the problem is that I may have a ListFragment that needs to start another fragment, it uses the activity's addFragment method
7:50 PM
Kevin M.
I guess I was wondering if anyone has come up with a good framework for this communication
Mark M.
I am sorry, but I do not know how to answer that beyond what I already have
let me swing back to Gabriele, and I will be with you again shortly
Gabriele: do you have a question?
Gabriele
Now I'm using android.net.conn.CONNECTIVITY_CHANGE, anyway how can I distinguish when I'm connected to wifi and I can use the network?
Kevin M.
I have to take off, thanks
Mark M.
EXTRA_NETWORK_INFO will have a NetworkInfo telling you the connection type (WiFi, etc.)
the NetworkInfo will also tell you the status of that connection
Gabriele
if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
then I check if connected?
Mark M.
ConnectivityManager, not WifiManager
right, the NetworkInfo has isConnected()
and the action is android.net.conn.CONNECTIVITY_CHANGE
Gabriele
yes the action is right
Mark M.
(or ConnectivityManager.CONNECTIVITY_ACTION)
7:55 PM
Mark M.
Kevin: do you have another question?
OK, if anyone has a question, go ahead
8:00 PM
Gabriele
View paste
boolean isNoConnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
//connected
}
is this right?
Mark M.
if isNoConnected is true, you would not need to continue with the rest
Gabriele
yes, I've added an if with a return
Mark M.
otherwise, yes, that seems about right
Gabriele
anyway I'm trying this code and I'm never getting connected :O
Mark M.
how are you testing it?
8:05 PM
Gabriele
I'm trying it with ide/xoom
deactivating my wifi and enabling it
Mark M.
are you getting any broadcasts at all? do you hold the ACCESS_NETWORK_STATE permission?
Gabriele
yes I'm getting broadcasts
yes I have the permission
but every time isNoConnected is true
Mark M.
oh, wait
EXTRA_NO_CONNECTIVITY looks like it may be either true or simply not there
hence, use false as your default
Gabriele
ah
Mark M.
also, that extra is only there on a disconnect event, anyway
Gabriele
oh
now it's connecting, but two times
and not one!
8:10 PM
Gabriele
I'm reaching if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) { two times and it's true two times, why?
Kevin M.
has left the room
Mark M.
I have no idea
Gabriele
:O
isn't there a way to check which is the reason of the event received?
Mark M.
you can check the DetailedState of the NetworkInfo, perhaps
Gabriele
it says: Details CONNECTED
two times :O
Mark M.
sorry
I suppose when I use this, I do not care that I might get duplicate broadcasts
Gabriele
my problem is that, if there are duplicates, I'm launching my service two times
8:15 PM
Mark M.
since services are singletons, you will still only have one service
so, perhaps the service needs to be smart enough to not do more work in onStartCommand() if it is already doing the work, or something like that
Gabriele
and how can I check it?
anyway it seems that my s2 is giving me the broadcast only one time
so the problem maybe is the rom of my xoom
Mark M.
perhaps
ideally, your service knows whether or not it is doing some work already (e.g., holding onto the Thread you forked)
Gabriele
or maybe is due to android 4.2 (my s2 has 4.1)
anyway I need to check if it's already working
Mark M.
to be safe, I would recommend that
Gabriele
so I don't have to care about it
a static field is a bad thing?
8:20 PM
Mark M.
um, that depends on what it is and how you use it
static data members represent possible memory leaks
so the #1 thing is to make sure that your use of a static data member will not represent a runaway memory leak
Gabriele
I meant to check if the service is already running
Mark M.
the service would not need a static data member to see if it is running -- a regular data member will do
again, services are natural singletons
Gabriele
ah true
sorry, here it's late :P
so I can set it to true on onstartcommand and false before I finish my work
Mark M.
yes, though if that work is in a background thread, you may want to use an AtomicBoolean to be safe
8:25 PM
Gabriele
thank you very much Mark, good night or good day :P
Mark M.
good night!
8:30 PM
Gabriele
has left the room
Mark M.
turned off guest access

Thursday, March 7

 

Office Hours

People in this transcript

  • Gabriele
  • Kevin Moore
  • Mark Murphy