I've searched this and I can't resolve the problem, look at this code checked on other examples:
public class EntryActivity extends Activity {
public Boolean isNetAvailable(Context con) {
try{
ConnectivityManager connectivityManager = (ConnectivityManager)
con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
return true;
}
}
catch(Exception e){
e.printStackTrace();
}
return false;
this method works well when I call it in onCreate method, like this:
if (isNetAvailable(getApplicationContext())) {
Toast.makeText(EntryActivity.this,
"You have Internet Connection, wait a moment to sincronize" +
"Metar's stations", Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
} else {
Toast.makeText(EntryActivity.this,
"You Do not have Internet Connection",
Toast.LENGTH_LONG).show();
EntryActivity.this.startActivity(new Intent(
Settings.ACTION_WIRELESS_SETTINGS));
}
This only works for the first Internet connection Check! the problem is, if Wifi is off the user should leaves the app or turns Wi-Fi on.But, if He turns it on, I don't have access to that change and I can't advance to my other activity.
So, If in the beggining the internet connection status is down, i want keep checking if the User turns it On to advanve to other activity.
What can I do?
In you android manifest:
<receiver android:name="com.softteco.clivero.receivers.ConnectionReceiver" >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
And create class:
public class ConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean newConnectionState = isConnected(context);
}
private boolean isConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
boolean ifConnected = false;
for (int i = 0; i < info.length; i++) {
NetworkInfo connection = info[i];
if (connection.getState().equals(NetworkInfo.State.CONNECTED)) {
ifConnected = true;
}
}
return ifConnected;
}
}
It's one of the possible variants. Hope it will help you.
You don't need to keep checking for Internet connection changes via executing your code at intervals. Instead you need to define a broadcast recievers in your app to listen network changes.
See this answer
How can I monitor the network connection status in Android?
this may helps you Monitor network connections Wi-Fi, GPRS, UMTS, etc
public boolean isNetworkAvailable(Context context)
{
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info!=null;
}
also make sure you added require permission in Manifest File like
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
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 have a printer inspection app on android phone, its basic inspection form
Inspector can work on a printer inspection even if he has no internet connection,
Once the phone is back with reception/internet, I would like to submit the inspection.
I was thinking to design the app using an android service
so it will save the inspection details using sqlite, then when there is internet connection to resubmit the inspection .
But this require service to periodically check for internet. and will consume significant battery juice.
Is there a hook I can register for my app to notify the app or the service on internet connection?
Simple check for both Wi-fi and Mobile internet as follows...
in Manifest.xml :
<receiver android:name=".com.yourapp.ConnectivityChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
make a new BroadcastReceiver :
public class ConnectivityChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if(checkInternet(context))
{
Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show();
}
}
boolean checkInternet(Context context) {
ServiceManager serviceManager = new ServiceManager(context);
if (serviceManager.isNetworkAvailable()) {
return true;
} else {
return false;
}
}
}
and finally ServiceManager class :
public class ServiceManager {
Context context;
public ServiceManager(Context base) {
context = base;
}
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
** Don't forget to add permission to use the Internet in your manifest file :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Also check out this super cool article on vogella.com AndroidServices ...
I'm trying to check internet connectivity on recieving sms.
But my service is force closing while changing internet state. E.g If I've enabled wifi its working fine but when I disable wifi app becomes closed with error.
I'm using this code in onReceive function.
Here is the code.
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if(isConnected == true) {
Toast.makeText(context, "Internet", Toast.LENGTH_LONG).show();
}
else if(isConnected == false){
Toast.makeText(context, "No Internet", Toast.LENGTH_LONG).show();
}
Try to use this code
add permission in manifest file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class ConnectionDetector {
public static boolean isConnectingToInternet(Context context){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
call
ConnectionDetector.isConnectingToInternet(LoginActivity.this);
try to use this code
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
Also add Permission in Manifest File :
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
You can add Below
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Please Check this Demo
Android Detect Internet Connection Status
i use this code
Create new class, try
public class Network{
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
call
Network.isNetworkAvailable(Activity Name.this);
and use this permissions
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I want to check the internet connection before opening an intent. How can I do this? I am a beginner in this field. Any help will be appreciated.
This method checks whether mobile is connected to internet and returns true if connected and also you need to update your manifest file
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null) {
// There are no active networks.
return false;
} else
return true;
}
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Try Following code
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
Do add permission in the manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Use ConnectivityManager before starting new Intent, if isConnectedOrConnecting() returns true you may proceed with Intent.startActivity()
Don't forget android.permission.ACCESS_NETWORK_STATE in Manifest
Have a look at duplicate here for deeper explanation
Very Simple Copy this Function to Your Activity from which you want to Intent.
public boolean CheckInternet() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
return true;
}
return false;
}//end of check int
Now before Intent Simply call this function in if condition
if(CheckInternet()){
Intent intent=new Intent(this,NextActivity.class);
startActivity(intent);
}
else{
Toast.makeText(this, "No Internet.", Toast.LENGTH_SHORT).show();
}
Don't forget to add these lines in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Thanks.
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
}
}
}