5

After executing this line:

WifiManager man = ((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE));

A thread labeled "WifiManager" will show up. In the Java source file for WifiService.java line 203:

 HandlerThread wifiThread = new HandlerThread("WifiService");
 wifiThread.start();
 mWifiHandler = new WifiHandler(wifiThread.getLooper());

Problem is, every time our app is closed and reopened it creates a new thread, run it 5 times and you have 5 threads. Not sure if there is anyway to stop it?

EDIT

Changed to getApplicationContext to make sure the context it was accessing was consistent and all was well. I still get a thread labeled "WifiService," but I only get one thread over multiple runs.

accordionfolder
  • 573
  • 4
  • 17
  • 1
    I feel like your real problem is you have some context issues, so I won't answer. But if you want to hack your way through this issue you can find your thread by [getting](http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java) an array of threads from the stack and then searching by thread.getName() equals 'WifiService' since ThreadHandler extends Thread. Then do something like [this](http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java). – shibbybird Jan 12 '13 at 02:41
  • 1
    Who owns the threads you are seeing? Do they have the same user and process id as your application or the system process? Are you looking at this on a particular device or in the emulator? Does "closed and reopened" mean going BACK/HOME and opening, or Force Stopping the process? If the former, what happens when you do the latter? – devunwired Jan 18 '13 at 04:59
  • 1
    looks like a defect: http://code.google.com/p/android/issues/detail?id=43006 – user1159819 Feb 02 '13 at 02:13

3 Answers3

3

I believe you are creating a new WifiManager in your started/stopped (Context) Activity.

A note from Context.getSystemService()

Note: System services obtained via this API may be closely associated with the Context in which they are obtained from. ...

Also from ContextImpl.java:1478 and :227

@Override
public Object getSystemService(String name) {
    ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
    return fetcher == null ? null : fetcher.getService(this);
}



...
service = cache.get(mContextCacheIndex);
if (service != null) {
    return service;
}
...

It uses a map to cache system services, so I believe if you use the same context like Application, you wouldn't run into this problem. I am not sure if this is the right way of solving this problem however, if having threads laying around a bigger issue for you, it may worth while.

auselen
  • 27,577
  • 7
  • 73
  • 114
  • I feel that somehow I should've read that before.... I'm trying it now. I believe that's what ShibbyBird was getting at, but didn't elaborate far enough for my dense self to get. Will report back shortly. – accordionfolder Jan 18 '13 at 17:14
2

When you get the instance of a system service using Context.getSystemService(), you are not calling the constructor of the service. Instead, you are actually getting an instance of the service using IBinder so as to do a remote procedure call on it. So the constructor of WiFiService.java will not be called every time you get an instance of it. Where exactly are you seeing this thread pop up?

StarNix
  • 390
  • 3
  • 7
  • Every time we do a full startup/shutdown cycle we get a new "WifiManager" thread. If you step through the code on a device after calling the getsystemservice you will immediately see a new thread start. I've dug through the source code and the documentation and (as far as those parts are concerned) we are doing proper "clean up." But we get a new thread per start/stop cycle with no way of getting rid of them. – accordionfolder Jan 12 '13 at 23:53
1

I hope your application is the only application which is accessing WifiManager. Please check at the same time with some dummy application which access WifiManager; in that case it should not create a new thread.

Munish Katoch
  • 517
  • 3
  • 6