I am making an android app which will maintain login and logout details of person using wifi connection. Person gets logged in whenever connect to WIFI and logout if connection lost.
I want to track wifi log details of android device and store in database. Is there any solution?? please help!
Add this permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Use BroadcastReceiver.
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)
Log.d("WifiReceiver", "Have Wifi Connection"); // here login
else
Log.d("WifiReceiver", "Don't have Wifi Connection"); // here logout
}
};
and add this in manifest file.
<receiver android:name=".WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
Related
i wrote simple BroadcastReceiver to detect internet connection and check internet available to send data to server, only return true on first opening application, it mean, after open application this BroadcastReceiver can be detect correctly connecting to internet, after that on each turn on/turn off wifi i get only false and it doesn't correct and must be true or false
public class ConnectionListener extends BroadcastReceiver {
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
#Override
public void onReceive(Context mContext, Intent intent) {
Log.e("check ", isNetworkAvailable(mContext) + "");
if (isNetworkAvailable(mContext)) {
EventBus.getDefault().post(new EventNetworkConnectionStateEvent(EventNetworkConnectionStateEvent.ConnectionState.connected));
} else
EventBus.getDefault().post(new EventNetworkConnectionStateEvent(EventNetworkConnectionStateEvent.ConnectionState.disconnected));
}
}
manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
receiver:
<receiver android:name="..Broadcasts.ConnectionListener">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
I am writing an application, which needs to check Ethernet(RJ45) connectivity.
for this purpose am using a broadcast receiver, but it is only working for Connected State, and my application hangs when I disconnect the Ethernet (RJ45).
here is my Broadcast receiver code and manifest.
public class NetworkChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean isConnected ;
ConnectivityManager networkConnectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = networkConnectivity.getActiveNetworkInfo();
if(networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET)
isConnected = true;
else
isConnected = false;
}
}
Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<receiver
android:name=".NetworkChangeReceiver"
android:label="NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
As far as I know you should do a null-check for the active network info. It is stated on the API ref of the android that
public NetworkInfo getActiveNetworkInfo ()
Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always check isConnected() before initiating network traffic. This may return null when there is no default network.
Please have a look at the below code.
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager conMan = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null) {
if (netInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
// is connected
}
}
}
Trying to check Internet connection in my app.
There is a code of my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
And there is a handle class:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null && intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE"));
{
Log.d("myLogs", "Network connectivity change");
if (intent.getExtras() != null) {
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(
ConnectivityManager.EXTRA_NETWORK_INFO);
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
Log.i("myLogs", "Network " + ni.getTypeName() + " connected");
}
}
if (intent.getExtras().getBoolean(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.d("myLogs", "There's no network connectivity");
}
}
}
}
In my Logcat i get a picture like a:
04-11 23:24:48.021: D/myLogs(10261): Network connectivity change
04-11 23:24:48.021: I/myLogs(10261): Network WIFI connected
04-11 23:24:48.202: D/myLogs(10261): Network connectivity change
04-11 23:24:48.202: I/myLogs(10261): Network WIFI connected
So, receiver called twice. Why? There is some problem with all types of connections.
You may also need to check to see if the intent being received is one of the Sticky events by checking isStickyBroadcast in your reciever. If that is true, then you may ignore it, and continue on. You get sticky broadcasts as soon as the receiver is registered.
http://developer.android.com/reference/android/content/BroadcastReceiver.html#isInitialStickyBroadcast%28%29
Explained in the docs:
"Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor this broadcast only when you've previously suspended updates or downloads in order to resume them. It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored."
Edit:
Forgot to add...a broadcast receiver only to know if the device is connected is a bit of an hoverhead. From the same docs:
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
How can I open my application when an user enters a zone that has wi-fi? Is this possible? Suppose my application is onPause() state (means My Device's homescreen). now when device connected with wifi. it will automatically open my application.
Try add broadcast receiver and listen network changes, when wi-fi connected start your activity. Something like this solution
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (((null != wifi)&&(wifi.isAvailable())) || ((null != mobile)&&(mobile.isAvailable()))){
Intent uplIntent = new Intent(context, YourActivity.class);
uplIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uplIntent);
}
}
}
And add to manifest
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
What I can imagine is an overwriting of the onPause() Method like this:
#Override
public void onPause() {
String conn_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager) getSystemService(conn_context);
if (wifi.isWifiEnabled())
{
super.onResume();
}
else
{
super.onPause();
}
}
But you must also figure a way to handle the real onPause event.
Maybe doable with the Tasker app from Play Store (not free though). Or you can create a Service (http://developer.android.com/guide/components/services.html) that will have code outlined in the other answers and then launch your app (Activity) when wifi is available.
I know how to checking if internet is available or not in the activity, my requirement is, actually i am storing some values in DB when the device in offline ( no internet), when it comes to online have to pass DB values to the server with out user interactivity. how can i achieve this.
i got this example How to check the Internet Connection periodically in whole application? but it's working only for one activity , how to make it as global.
Use this in your receiver
<receiver android:name=".UpdateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Your UpdateRecieiver
public class UpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();
if (isConnected)
Log.i("NET", "connecte" +isConnected);
else Log.i("NET", "not connecte" +isConnected);
}
}
try out this function
public static boolean isInternetAvailable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
and in the manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
enjoy..