无线连接时收到通知

时间:2016-01-27 03:36:49

标签: android android-wifi wifimanager wificonfiguration

我正在使用广播接收器在Wi-Fi连接到我的设备时收到通知。我检查特定的SSID以运行POST请求以登录到该特定网络。但是我会在10秒后收到通知。我确实在onReceive()的开头放了一个Log语句,并且它被执行得很晚。我如何尽快实现这一目标?

每次我的应用程序活着时,我是否需要注册接收器?(顺便说一下,我这样做)

我还在运行一项服务,在手机启动时注册接收器。

的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));

谢谢!

0 个答案:

没有答案