Hello Friends,
Note: Declare following network permission in your
Note: Declare following network permission in your
Androidmanifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1. MainActivity.java
package com.mukesh.networkcheck;
import com.mukesh.netwoekcheck.R;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isEnabled = isNetworkEnable();
displayNetworkState(isEnabled);
if (!isEnabled) {
buildAlertMessageNoGps();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
displayNetworkState(isNetworkEnable());
}
}
private void displayNetworkState(boolean isEnabled) {
String status = "Your Network is "
+ (isEnabled ? "Enabled" : "Disabled");
((TextView) findViewById(R.id.text_gpsstatus)).setText(status);
}
private boolean isNetworkEnable() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
Log.v("TAG", "Internet Connection Not Present");
return false;
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.network_availability_title);
builder.setMessage(R.string.network_availability_message);
builder.setCancelable(false);
builder.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
final int id) {
launchNetworkOptions();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
builder.create().show();
}
private void launchNetworkOptions() {
startActivityForResult(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS),
REQUEST_CODE);
}
}
2. AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mukesh.netwoekcheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mukesh.networkcheck.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Download Source Code: Network Check
source code link is broken
ReplyDeletePlease Update