I am using org.apache.http in my app to connect with internet but when I am running my app in Samsung galaxy duos y gt-1602, then it is not able to use Sim GPRS while it is running properly with WiFi.
Can anyone suggest me what could be the problem or possible solution.
Is there any lines of code related to Network connectivity type in your pgm?
Following if a simple method to detect connectivity.
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++) {
Log.d("Connectivity", "info:"+info[i]);//Here you can check network info if available
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
Related
In my android app I have to gather a lot of information regarding the cell tower
like Network name, Network code etc etc but before I start capturing all those
things I want to make sure that a cell tower connections exists
i.e. GSM or CDMA, but I want to ensure that there is a connection. Otherwise
I would just like to return.
For example, as we do it in case of Wi Fi. With the help of WifiManager's isWifiEnabled() method we ensure first hand whether we have Wifi running or not.
Similar to that, for Cell Tower.
As described in this post, you can find as below
boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
// True if the phone is connected to some type of network i.e. has signal
Another way from same link, but not marked as solution, is to check for network operator field as below
public static Boolean isMobileAvailable(Context appcontext) {
TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);
return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);
}
Try this:-
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
boitealerte(this.getString(R.string.alert),"getSystemService rend null");
} else {
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;
}
It will return true if the network is available.
I am using some code to detect network state and it works fine but sometimes the device is connected to wi-fi network but actually there is no internet .. i mean it is connected to router but the line is not working .. for example, when telephone line is cut off .. something like that ..
now how can i detect that ??
This is my code :-
public boolean isConnectingToInternet(){
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;
}
I want to check if a particular device has hardware support for 4G networks.
I will elaborate the issue...
In the application we have a settings page where user can make selection and allow application to run only in selected networks.
Eg. User can select that app will run only in WiFi network or only in 3G network etc.
There are CheckBox preferences for all networks WiFi, 2G, 3G 4G etc.
Now if the device doesn't have the support for 4G network, I want to hide the 4G selection checkbox.
All the remaining functionality is complete. I am struck on just this issue that how to detect if device support 4G or not?
Please note that I want to detect hardware support for 4G on the device and NOT the 4G connection is connected or so.
Any help is greatly appreciated.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = cm.getAllNetworkInfo();
for(int i=0; i <info.length; i++){
Log.i("netinfo"+i, info[i].getType()+"");
Log.i("netinfo"+i, info[i].getTypeName());
Log.i("netinfo"+i, info[i].getSubtype()+"");
Log.i("netinfo"+i, info[i].getSubtypeName());
}
Use this piece of code and get all available options on the device. I think you should be able to get all capabilities of the device.
and network type info is present in http://developer.android.com/reference/android/telephony/TelephonyManager.html.
I think network types added from API level 11 are for 4g. Check it yourself.
This might work. It should check for WiMax 4g:
private boolean is4gavailable() {
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.isConnectedOrConnecting() || wifiInfo.isConnectedOrConnecting() || wimaxInfo.isConnectedOrConnecting();
}
else {
return mobileInfo.isConnectedOrConnecting() || wifiInfo.isConnectedOrConnecting();
}
}
Try looking at this for a little more clarification.
Context context = MainActivity.this;
public synchronized static boolean isNetAvailable(Context context){
boolean isNetAvailable=false;
if ( context != null ){
ConnectivityManager mgr = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if ( mgr != null )
{
boolean mobileNetwork = false;
boolean wifiNetwork = false;
boolean wiMaxNetwork = false;
boolean mobileNetworkConnecetd = false;
boolean wifiNetworkConnecetd = false;
boolean wiMaxNetworkConnected = false;
NetworkInfo mobileInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo wiMaxInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
if ( mobileInfo != null )
mobileNetwork = mobileInfo.isAvailable();
if ( wifiInfo != null )
wifiNetwork = wifiInfo.isAvailable();
if(wiMaxInfo != null)
wiMaxNetwork = wiMaxInfo.isAvailable();
if(wifiNetwork == true || mobileNetwork == true || wiMaxNetwork == true){
mobileNetworkConnecetd = mobileInfo.isConnectedOrConnecting();
wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
wiMaxNetworkConnected = wiMaxInfo.isConnectedOrConnecting();
}
isNetAvailable = ( mobileNetworkConnecetd || wifiNetworkConnecetd || wiMaxNetworkConnected );
}
}
return isNetAvailable;
}
Check for value of isNetAvailable is true in all the 3 cases this works for me, wish work for u too
Note : 4G availability will be introduces API level 8 onwards.
I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.
My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.
The code
protected boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
} //end checkInterneConnection method
Thanks for your time.
Mel
You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable: http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29
This piece of code will check whether your device Internet conecction, If the signal is Poor it will show a Toast other wise not,
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = conMan.getActiveNetworkInfo();
if(Info == null){
Toast.makeText(RegisterActivity.this,"Network Connection Failed! ", Toast.LENGTH_SHORT).show();
}
You can try ping http://google.com or doing something like this to confirm it's ok to visit internet.
You should try this:
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)
m_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;
}
And to check only wifi is simpler:
private boolean isWifiConnected() {
int WIFI_STATE = wifi.getWifiState();
if(WIFI_STATE == WifiManager.WIFI_STATE_ENABLED)
return true;
return false;
}
This code will really test the internet connection:
public static boolean isInternetAvailable() {
try {
InetAddress address = InetAddress.getByName("google.com");
return address.isReachable(2000); //This really tests if the ip, given by the url, is reachable
//return !address.equals(""); //This just tests if the IP is available but it could be taken in a previous request when internet was available
} catch (Exception e) {
Log.d("Internet check", "Unable to reach the url: "+url);
}
return false;
}
I want to check the network connectivity in android application.so i inserted the following code
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
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;
}
when i removed network cable in my computer the program crashed.but when i disable Airplane Mode in Emulator,it correctly shows "NETWORK NOT AVAILABLE".
How to we actually check?
Did you try it on actual device.
My inference from your post is, you are trying on an emulator.
And I think emulator is linked to the network interface of the system
Hence it could have failed.
Other change what I propose :
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info.getState() == NetworkInfo.State.CONNECTED )
return true