Checking internet connectivity in android - android

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"/>

Related

App crashing during checking internet connectivity type

I have written code to check the network connectivity type as follows:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile
NetworkInfo.State mobile = conMan.getNetworkInfo(0).getState();
//wifi
NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState();
I have also added the following permission to the AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
But when I run the application it crashes. Does anyone know what is causing this?
Have you added these two permissions in your manifest file, if not add these:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can't tell the actual problem or solution as you haven't posted any error log. Though you can try this code.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Android - Checking internet connection before starting an intent

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.

Getting connection returns false even when the device has connection

I have an app that has an alarmlistener that returns position and send it to a server, but firts it checks the internet connection. This is the method that checks the connection:
public boolean hasInternetConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()
&& networkInfo.isConnected()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Permissions on the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="pe.com.hacom.taxitrack.mgr.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
And it has been working fine, but when I tried it on a Motorola XT316 it didn't work, it always return false, even when the device has connection, I check it openning the internet browser and it shows the webpage normally, but the alarm still returning false with that method.
I tested it in other devices and it works fine with all, but that Motorola, any idea what is happening? is that a hardware bug?
And the most rare thing is that sometimes I can get the device in DDMS for the logs on the debug but almost always it appears "Offline".
I have installed the driver that is in the device, also uninstalling it and installing the universal driver of motorola from the webpage, and its working with Motoroal RAZR's but not with the XT316.
This might be happening because the device have some sort of battery management system, which disables internet connection, and enables it when you open the browser, but it doesn't work when this Alarm is triggered.
I was testing a lot and I found that many times when I'm asking for connection the NetworkState is "Connecting" so I change the method where I ask for connection.
public boolean hasInternetConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting())
{
return true;
} else {
return false;
}
} else {
return false;
}
}
I read the documentation on Google's developer page and found that it is not recommended to use the isConnectedOrConnecting() method if you're sending or receiving data from the internet, but I think that the BatteryManagment of some devices maybe cut the connection or maybe it doesn't get connected because the 3G network in my city sucks.
I know this is not the best practice but is working, and it's strangely sending and receiving data even when the network state is "connecting".

Test Internet connection changes-android

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>

How to make call to service from receiver depends on Internet connected or not?

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..

Categories

Resources