Maintain state of application when Internet connection loses in android - android

In my app, internet is being used. What I have to do is, I have to check internet connection when my application runs. When I first start the app, it checks, whether internet connection is available or not in device. Internet connection can be through Wi-Fi or Mobile. If, any of these is available then and only then my app should work, else it should prompt message. I have done this till. Below is my code...
public final boolean isInternetOn() {
ConnectivityManager connec =
(ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED )
Toast.makeText(this, " Connected ", 1000).show();{
return true;
} else if (
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {
Toast.makeText(this, " Not Connected ", 1000).show();
return false;
}
return false;
}
I have done till this. But what I want is, when user moves to second activity, and if internet connection loses, then state of app should be maintained. And when internet connection is activated again, then app should work properly from Second activity onward. What needs to be done?

Related

android:how to detect network is connected to internet

I have this code to check for internet connection.
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
//if network is active and have internet connection
if(info != null && info.isConnected()==true){
//Some code
}
//if network is inactive or doesn't have internet connection
else if(info == null || info.isConnected()==false){
Context context = getApplicationContext();
CharSequence text = " Bad internet connection.";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
When I start the program,everything works properly with turn on internet connection,but when I pull out the internet cable from my router and in my app still have turn on wifi the app get true with this (if(info != null && info.isConnected()==true)) and crash.I don't know why this code get true.
Use this for check condition of network:
if (info!=null && info.isAvailable() && info.isConnected()) {
return true;
} else {
return false;
}
}

Android App crashes on 4G internet connection but works fine on GSM mode Only

I have created an android application which user Http Post and Get calls to read and write data from web server. Nothing too flashy also. My phone/SIM has 4G. Every time the app tries to connect, thing go slow and most of the time the app crashes. So I changed settings to GSM only. Guess what? The app works much better. What could be the possible reasons for it?
Use the following method in order to detect all available type of networks at your client's disposal :
public static boolean checkNetworkRechability(Context context) {
Boolean bNetwork = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
for (NetworkInfo networkInfo : connectivityManager.getAllNetworkInfo()) {
int netType = networkInfo.getType();
int netSubType = networkInfo.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else {
bNetwork = false;
}
}
if (!bNetwork) {
Log.i(TAG, "You are not in network");
}
Log.i(TAG, "bNetwork : " + bNetwork);
return bNetwork;
}

Android - How to check internet Access, not just connectivity to wifi? [duplicate]

This question already has answers here:
Detect if Android device has Internet connection
(16 answers)
Closed 9 years ago.
I tried the below code to check whether my mobile is connected to a wireless network and it works well when I want to know if my mobile is connected to the network, but it fails to give information about the internet access ... something like "Pinging" any website.
Actually I followed many links but still no answer, so I'll be so thankful if anybody can help.
Thanks in Advance.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast t = new Toast(getApplicationContext());
if (isInternetOn()) {
// INTERNET IS AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"Network is Available", Toast.LENGTH_LONG).show();
}
else {
// NO INTERNET AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"No Network Available", Toast.LENGTH_LONG).show();
}
}
public final boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
return true;
} else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}}
EDIT:
Follow below link it contains a great answer for Ping google server and get result
https://stackoverflow.com/a/16458623/1239911
Actually I used the same function isInternetOn() but I removed the connecting condition.
It had to check the status of connection if connected or not and if it is trying to connect. This didn't work for me, so I removed connecting status checking and then it worked.
Thanks for all replies.
public final boolean isInternetOn()
{
ConnectivityManager connec = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
{
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
return true;
}
else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED )
{
return false;
}
return false;
}
see the sample:
public static boolean isWifiEnabled() {
if ( !gWifiManager.isWifiEnabled()) {
if (mCanShowWifiToast) {
new Thread(mWifiToastControl).start();
G.gHandler.post(mNoWifiRunnable);
}
return false;
} else {
int linkspeed = gWifiManager.getConnectionInfo().getLinkSpeed();
if (linkspeed < 5) {
if (mCanShowWifiToast) {
new Thread(mWifiToastControl).start();
G.gHandler.post(mNoWifiRunnable);
}
return false;
}
}
return true;
}
You can use BroadcastReceivers that will trigger when the connection status change: there is a similar question with an answer: Broadcast intents for bluetooth, wifi and ringer mode

how to check net connection in android phone [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android - detect whether there is an Internet connection available
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?
In my cell when I run my app then this application is crashed because net connection is not there.so how to give the message that the connection is not there like. or on your wif
please help me
thank you
See below code.
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
// internet connection is wroking
} else {
return false;
// internet connection is not wroking
}
Use this method in your Activity and call this when you want to know whether the net is available or not.
public boolean isInternetOn(Context ctx) {
this.mContext = ctx;
ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
State connected = NetworkInfo.State.CONNECTED;
State connecting = NetworkInfo.State.CONNECTING;
State disconnected = NetworkInfo.State.DISCONNECTED;
State info0 = Connect_Manager.getNetworkInfo(0).getState();
State info1 = Connect_Manager.getNetworkInfo(1).getState();
// ARE WE CONNECTED TO THE NET
if (info0 == connected || info0 == connecting || info1 == connecting
|| info1 == connected) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
// Toast.makeText(this, connectionType + " connected",
// Toast.LENGTH_SHORT).show();
Log.d("Internet", "Connected");
return true;
} else if (info0 == disconnected || info1 == disconnected) {
Log.d("Internet", "DisConnected");
// System.out.println("Not Connected");
return false;
}
return false;
}
Now to check Internet Conncetion,
if (isInternetOn(this))
{
//Do stuff
}
else
{
//show alert
}

TelephonyManager.getNetworkType() and HSDPA problems :(

I have been using the following code to establish which network the device is using :
TelephonyManager tempManager;
tempManager= (TelephonyManager)myContext.getSystemService(Context.TELEPHONY_SERVICE);
int result = 0;
if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) //do we have a UMTS connection ?
{
result = 2;
}
else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS) //or is it just a shabby 2g connection ?
{
result = 1;
}
else if(tempManager != null && tempManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UNKNOWN) //or is it just a shabby 2g connection ?
{
result = 4;
}
return result;
It works pretty well unless I get on a HSDPA connection, in that case it will always return 0 as a result, which in my case makes my software think it has no connection at all :(
Anyone who knows whats happening, has some experience regarding this and most importantly has some solution to this problem ???
Thanks in advance
There is a enum also for HSDPA
To check if there is connection and also get a type, I would rather user getActiveNetworkInfo and isConnected. It returns null when there is no connection. You can also check the type of connection by getType and getSubtypeName methods or you can mix with your approach.
This is the code I am using. It will safely check for a connection and then return the connection type. For my application, I only care about Wifi, Mobile, or not connected so that is what this function will return. Change for your own situation.
//Check weather Internet connection is available or not
public int checkConnectionType()
{
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetInfo = conMgr.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.isAvailable() && activeNetInfo.isConnected())
{
int type = activeNetInfo.getType();
if (type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_MOBILE_DUN
|| type == ConnectivityManager.TYPE_MOBILE_HIPRI || type == ConnectivityManager.TYPE_MOBILE_MMS
|| type == ConnectivityManager.TYPE_MOBILE_SUPL || type == ConnectivityManager.TYPE_WIMAX)
{
return ConnectivityManager.TYPE_MOBILE;
}
else if (type == ConnectivityManager.TYPE_WIFI)
{
return ConnectivityManager.TYPE_WIFI;
}
else
{
// Unknown connection type, so to be safe say mobile
return ConnectivityManager.TYPE_MOBILE;
}
}
else
{
// return not connected
return -1;
}
}
You will need this permission in your app's manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Categories

Resources