This is kind of several questions in one. Basically I have a tabbed application w开发者_运维百科ith three tabs. Each tab will need to access a web service and the wifi API. I want to simplify this by creating two static wrapper classes for all this shared functionality so that all three activities can use the same static classes.
I am fairly unpracticed in android/java so I am looking for a few pointers here.
As an example, here is my Wifi class, it simply registers the listeners and posts scan results to them whenever it receives them. It is up to the subscriber to decide if they want to pay attention to them or not.
public final class WifiManagerActivity extends Activity {
private static WifiManager _wm;
private static List<OnWifiRecievedListener> _listeners;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (!_wm.isWifiEnabled())
_wm.setWifiEnabled(true);
_listeners = new ArrayList<OnWifiRecievedListener>();
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
publishResults(_wm.getScanResults());
_wm.startScan();
}
}, i);
_wm.startScan();
}
private static void publishResults(List<ScanResult> results){
for (int i = 0; i < _listeners.size(); i++)
_listeners.get(i).onWifiRecieved(results);
}
public static void addWifiRecievedListener(OnWifiRecievedListener owrl) {
if (!_listeners.contains(owrl))
_listeners.add(owrl);
}
public static void removeWifiRecievedListener(OnWifiRecievedListener owrl){
if (_listeners.contains(owrl))
_listeners.remove(owrl);
}
}
Is this valid? Or am I ignorant of the lifecycle of android applications? Any pitfalls in my way?
Also, if the WifiManagerActivity is on a separate thread, how does it handle calling a method that needs to work with variables on another thread? In C# I would use a dispatcher to invoke it, but with java/android??
In summary:
1) Any problems or suggestions with my current plan?
2) How do I call a method from one thread and have it alter variables that belong to another thread?Thanks!
You have number of issues with your approach. Activity
objects are usually used to display UI. In your case you may want to implement entire WifiManager as broadcast receiver instead of registering receiver inside activity's onCreate
.
Secondly, what are your expected listeners? Are they other activities? I assume that your intention is to change something in UI when wifi state changes. Since only one activity can be active at a time, it does not make much sense.
Overall, I propose two design options:
Use intermediate service if you need to respond to WiFi changes regardless of top level activity.
- Register broadcast receiver to watch WiFi state
- Create service that would have following methods. One to respond to WiFi change messages and another one to register callback from top level activity. Possibly extra one to deregister callback.
- In your activity connect to service and register callback
Messenger
inonResume
method. Deregister inonPause
.
You will also need to refresh state of activity when it comes to the top.
- Just register broadcast receiver once your activity comes to the top of the stack.
Sounds like you want that stuff running in a Service
rather than an Activity
.
http://developer.android.com/reference/android/app/Service.html
精彩评论