2

I am using broadcast receiver to get notified whenever Wi-Fi gets connected to my device. I checking for specific SSID to run a POST request to log-in into that specific network. But I am getting notified about 10sec later. I did put a Log statement at start of onReceive() and its gets executed late. How do I achieve this as soon as possible?

Do I need to register the receiver everytime my app becomes alive?(btw I am doing it)

I am also running a service which registers the receiver when phone is booted.

Manifest.xml

    <receiver android:name=".Services.LoginWhenConnected">
        <intent-filter android:priority="999">
            <action android:name="android.net.wifi.STATE_CHANGE" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
        </intent-filter>
    </receiver>

onReceive()

 @Override
 public void onReceive(final Context context, Intent intent) {
 Log.d("receive","receiveSuccess");     
 if (!TabActivity.isActivityVisible()) {
        networkName = new SharedPrefs(context).getValue(Config.networkNamePREF, "Hexagon").trim();
        try {
            this.context = context;
            if (checkConnection()) {//this method just checks for specific SSID
                doAsync(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


private boolean checkConnection() {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMan.getActiveNetworkInfo();
    if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (//compare ssid names) {
            return true;
        }
    }
    return false;
}  

registerReceiver:

 registerReceiver(new LoginWhenConnected(),
                new IntentFilter(
                        ConnectivityManager.CONNECTIVITY_ACTION));

Thanks!

Nikhil Verma
  • 1,777
  • 1
  • 20
  • 41

0 Answers0