Android Open Source - AndroidNetworkStatus Network Changes Receiver






From Project

Back to project page AndroidNetworkStatus.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroidNetworkStatus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.diolan.netstat.data;
//  w w w  .j  a  v a 2s. co m
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;
import android.util.Log;

import com.diolan.netstat.data.DataEntry;
import com.diolan.netstat.data.DatabaseService;

import java.util.Date;

/**
 * Created by d.barkalov on 31.07.2014.
 */
public class NetworkChangesReceiver extends BroadcastReceiver {

    public static final String EXTRA_WIFIDATAENTRY = "EXTRA_WIFIDATAENTRY";

    @Override
    public void onReceive(Context context, Intent intent) {

        final String action = intent.getAction();

        if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            String eventInfo = null;
            String event;
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            Log.v("WifiStateChangesReceiver", "info=" + info.toString());
            event = info.getState().toString() + " " + info.getTypeName();
            eventInfo = info.toString();
            if (info.getState() == NetworkInfo.State.CONNECTED && (info.getType() == ConnectivityManager.TYPE_WIFI)) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                eventInfo+=  " ConnectionInfo=" + wifiInfo.toString();
                eventInfo+=" IpAddress:" + Formatter.formatIpAddress(wifiInfo.getIpAddress());
            }

            Intent serviceIntent = new Intent(context, DatabaseService.class);
            serviceIntent.setAction(DatabaseService.ACTION_INSERT);
            serviceIntent.putExtra(EXTRA_WIFIDATAENTRY, new DataEntry(new Date().getTime(), event, eventInfo));
            context.startService(serviceIntent);
        }

    }

}




Java Source Code List

com.diolan.netstat.ConfirmDialog.java
com.diolan.netstat.NetStatActivity.java
com.diolan.netstat.NetStatFragment.java
com.diolan.netstat.NetStatListAdapter.java
com.diolan.netstat.data.DataEntry.java
com.diolan.netstat.data.DatabaseService.java
com.diolan.netstat.data.NetStatContentProvider.java
com.diolan.netstat.data.NetworkChangesReceiver.java
com.diolan.netstat.data.StatDbHelper.java