In mobiles inter net speed is very slow, and we can get net connection through Blue Tooth
or WI FI .
so before perform any net Work based applications first we check net is properly working or
not .
in emulator for Enable and disable net work short cut key is : F8
Android API provides Excellent Packages for Net Work Based Applications
↳ android.net.ConnectivityManager :
================================
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
The primary responsibilities of this class are to:
1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
2. Send broadcast intents when network connectivity changes
3. Attempt to "fail over" to another network when connectivity to a network is lost
4. Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
context=getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
public boolean getBackgroundDataSetting () :
------------------------------------------
Returns the value of the setting for background data usage. If false, applications should not use the network if the application is not in the foreground. Developers should respect this setting, and check the value of this before performing any background data operations.
All applications that have background services that use the network should listen to ACTION_BACKGROUND_DATA_SETTING_CHANGED.
Returns
* Whether background data usage is allowed.
↳android.telephony.TelephonyManager
===================================
Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.
You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE).
Note that access to some telephony information is permission-protected. Your application cannot access the protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the the methods through which you access the protected information.
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
↳android.net.NetworkInfo
========================
Describes the status of a network interface of a given type (currently either Mobile or Wifi).
Before Running Any Applications in Emulator First we should add permissions to Manifest File
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
CheckNet.java :
============
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class CheckNet extends Activity {
private Context cuContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
cuContext = getApplicationContext();
ConnectivityManager connectivityManager = (ConnectivityManager) cuContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) cuContext
.getSystemService(cuContext.TELEPHONY_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
NetWorkStatus ns = new NetWorkStatus(connectivityManager,
telephonyManager);
if (ns.isNetWorkAvilable()) {
Toast.makeText(getApplicationContext(), "Connected ",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), " not Connected ",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.w(" error ", e.toString());
}
}
}
NetWorkStatus.java
--------------------------------
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;
public class NetWorkStatus {
ConnectivityManager mConnectivityManager;
TelephonyManager mTelephonyManager;
public NetWorkStatus(ConnectivityManager connectivityManager,
TelephonyManager telephonyManager) {
mConnectivityManager = connectivityManager;
mTelephonyManager = telephonyManager;
}
public boolean isNetWorkAvilable() {
NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
if (info == null || !mConnectivityManager.getBackgroundDataSetting()) {
return false;
}
int netType = info.getType();
int subtype = info.getSubtype();
Log.w(" network values ", " netType :" + netType + " subtype "
+ subtype + " ");
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& subtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephonyManager.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
}
}
If Connected Net Sucessfully |
If Net Is Not Connected |
For Downloading Source Click Me
No comments:
Post a Comment