Skip to content Skip to sidebar Skip to footer

Android: Waiting For An Action

So someone asked me to write an app for him that logs starts and stop, bssid, local ip, ssid and start/stop time when he connects to a WiFi access point. I did this by: public voi

Solution 1:

Is wait() inefficient ?

Yes this totally not wrong.

Read about BroadcastReceiver in android. Here is what you need to do:

protected void onResume() 
{
    super.onResume();

    IntentFilter intentFilter = new IntentFilter();

    intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");

    registerReceiver(myReceiver, intentFilter);
}

private BroadcastReceiver myReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        String str = intent.getAction();

        displayMessage("In myReceiver, action = " + str);
        Log.d("Settings", "Received action: " + str);

        if (str.equals("android.net.wifi.WIFI_STATE_CHANGED"))
            displayMessage("wifi changed...");

}};

Solution 2:

Why bother doing that while(connected){wait} thing? I would think you'd get another broadcast of SUPPLICANT_CONNECTION_CHANGE_ACTION:

Broadcast intent action indicating that a connection to the supplicant has been established (and it is now possible to perform Wi-Fi operations) or the connection to the supplicant has been lost. One extra provides the connection state as a boolean, where true means CONNECTED.

You could just check that extra every time to see whether you started or stopped the connection. That way your app doesn't even have to be running between start and stop.


Post a Comment for "Android: Waiting For An Action"