android tablet has no phone and code gives error when checking - android

I have an app that works fine on a android phone , but when I try to run it on the Nexus7 which has no phone the code fails with a force stop at the location indicated. What is the solution? How do I check to see if the feature is there and what should I do to solve this?
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
printi("oopsA",6);
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
printi("oopsB",6);
boolean isMobileConn = networkInfo.isConnected(); //<<<<FAILS HERE ON NEXUS 7

Your networkInfo is probably null. You have to test that before. This means you can't access to this type of connectivityManager.
Try this:
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = false;
if(networkInfo != null)
isMobileConn = networkInfo.isConnected();

I have faced same problem with Motorola Xoom, because it does not have connectivity support for ConnectivityManager.TYPE_MOBILE.
Following code is working fine for me :
ConnectivityManager connMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
return connMngr.getActiveNetworkInfo().isConnected();
}
catch (NullPointerException npe) {
return false;
}

Check permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Corrected Code as follows:
IsAPhone=0;
try{
boolean isMobileConn = false;
if(networkInfo != null){ isMobileConn = networkInfo.isConnected();IsAPhone=1;}
}
catch (Exception e) {}

Related

App crashes when there is no internet [duplicate]

This question already has answers here:
How to check internet access on Android? InetAddress never times out
(64 answers)
Closed 6 years ago.
My app is an rss feed and retrieves data when started and when refreshed but when there is no Internet connection the app crashes. How do I check for Internet connection and display a toast if there isn't any
You can use this function to check if there is internet
public boolean isConnected() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
You also need to add this to the manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
And then just check it
if(isConnected){
//Connected to the internet
}
else{
//Handle error and show toast
}
You can find a more detailed answer here
How to check internet access on Android? InetAddress never times out
To check is decive connected to network.
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
To check if internet is avalaibe
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
Use this function
private boolean isNetworkConnected() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Check Internet connected or not
if(isNetworkConnected){
}
And add below permission in your manifestfile
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

How to check internet access (send and receive data is successful ) on Android?

hi i want check when a phone or tablet connected to a network (mobile or wifi).
Do this network access to the internet and send and receive data?
i use this code:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI")) {
if (ni.isConnected()) {
Toast.makeText(context,"WIFI CONNECTED",Toast.LENGTH_LONG).show();
Boolean m = isOnline();
Toast.makeText(context,String.valueOf(m),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,"WIFI NOT CONNECTED AND AVAILABLE",Toast.LENGTH_LONG).show();
}
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
but it's always return false
(i test app in BlueStacks).
i test all codes that exist in stackoverflow
First of all, try this to check if you're really connected:
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
And to get the type of the connection, try this:
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
Check this image to see the other connection types.
Don't forget to add this line on your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
More information on the training.
More info on ConnectivityManager here.
EDIT: You could also try something like:
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
return info.isConnectedOrConnecting();
}
Second edit: Yeah, you could also do your "ping" approach. But... I don't think it's really necessary, you could simply try the solutions above...

how to check internet connection in Google nexus 7 (android 4.2)

I used following code for checking internet connection.
public static boolean checkNetworkConnection(Context context) {
boolean connected = true;
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState() == android.net.NetworkInfo.State.CONNECTED
|| connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState() == android.net.NetworkInfo.State.CONNECTED) {
connected = true;
} else
connected = false;
}
This code is working fine in below mobile. But it is not working in Google nexus 7 (android 4.2).
When I test this code in Google nexus 7 (android 4.2). I got error.
Null pointer exception in connection manager
For me it work:
public static boolean isInternetEnabled() {
ConnectivityManager conMgr = (ConnectivityManager) YourApp.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else
return false;
}
You will need this permission in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Check TYPE_MOBILE is null or not using below code.
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null

how to check a network is connected to wifi in android [duplicate]

This question already has answers here:
Detect network connection type on Android
(14 answers)
Closed 9 years ago.
Now I am developing an application. I checked the internet connection in android using ConnectivManager. I checked for Mobile 3g and for WIFI.I turn off the WIFI connection and checked it works as what i want. Then I turn on the WIFI connection but disconnected all networks in WIFI. Now I checked but it shows the WIFI is connected but I want to check whether a network is connected to WIFI when WIFI is on or not connected to WIFI when WIFI is on. I do not know how to code it. Can anyone please help with the required code?
Thanks in advance
public boolean isWifiEnabled(){
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
return false;
}
Don't forget to use the following permission
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
To Check if internet is connected via WiFi or mobile
public static boolean hasInternet(Activity a) {
boolean hasConnectedWifi = false;
boolean hasConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("wifi"))
if (ni.isConnected())
hasConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("mobile"))
if (ni.isConnected())
hasConnectedMobile = true;
}
return hasConnectedWifi || hasConnectedMobile;
}
With this piece of code you should be able to use the ConnectivityManager. From there you can check if it is connected or even available.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
To check network connection
public class CheckNetwork {
private static final String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}
}
}
}
In your activities
if(Checknetwork.isInternetAvailable(MainActivity.this)
{
// do something
}
Remember to add permission in manifest file
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
This also check's if you have connection to mobile network or wifi.

ConnectivityManager do not work on Motorola xoom (honeycomb 3.2)

I have a method call isNetworkAvailable()
to check if the user have enable wap/wifi/wimax this works for Android 2.1 to 2.3+.
But now a user of the app which uses honeycomb 3.2 on a Motorola xoom
rapport to me that he canĀ“t open the app.
In my android developer web-interface I can see this log-error: http://paste.ubuntu.com/811881/
private boolean isNetworkAvailable()
{
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileInfo = connec.getNetworkInfo(0);
NetworkInfo wifiInfo = connec.getNetworkInfo(1);
NetworkInfo wimaxInfo = connec.getNetworkInfo(6);
if (wimaxInfo!=null) {
return mobileInfo.isConnected() || wifiInfo.isConnected()|| wimaxInfo.isConnected();
}
else {
return mobileInfo.isConnected() || wifiInfo.isConnected();
}
}
See the whole Class/Activity here(line 276):
https://github.com/voidcode/Diaspora-Webclient/blob/master/src/com/voidcode/diasporawebclient/MainActivity.java
mobileInfo or wifiInfo could be null. On a wifi-only device I wouldn't be surprised if mobileInfo (ConnectivityManager.TYPE_MOBILE) is null.
I have faced same problem with Motorola Xoom, because it does not have connectivity support for ConnectivityManager.TYPE_MOBILE.
The following code is working fine for me:
ConnectivityManager connMngr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
return connMngr.getActiveNetworkInfo().isConnected();
} catch (NullPointerException npe) {
return false;
}

Categories

Resources