New Connection Detector class for kitkat,lollipop,marshmallow,Nougat or higher or lower version of Api. Android tutorial about detecting internet connection status in the android phone.The app will be detect whenever internet is connected or disconnected from internet.
Coding starting here
STEP 1. Create a new Project ConnectionDetector in Android Studio.
STEP 2. Entry this user permission of internet and access network state under -
1.manifests/AndroidManifest.xml :-use this user permission in androidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.amitrawat.connectiondetector"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
STEP 3. Create a java file ConnectionDetector.java under -
1.src/ConnectionDetector.java :-Connection Detector class that show internet is connected or disconnected.
ConnectionDetector.java:-
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
/** * Created by Amit Rawat on 4/21/2017. */public class ConnectionDetector {
private Network network;
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean hasInternetConnection() {
ConnectivityManager connectivity =
(ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
network = connectivity.getActiveNetwork();
capabilities = connectivity
.getNetworkCapabilities(network);
return capabilities != null
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connectivity.getAllNetworks();
NetworkInfo networkInfo;
for (Network mNetwork : networks) {
networkInfo = connectivity.getNetworkInfo(mNetwork);
if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
return true;
}
}
} else {
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;
}
}
|
STEP 4. Write code in java file MainActivity.java and xml file activity_main.xml under -
1.res/layout/activity_main.xml :-activity_main.xml is the layout file of MainActivity.java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.amitrawat.connectiondetector.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="turn on/off data access over mobile network or you can use wifi to check the app about internet connection status of android phone!here i am using data access over mobile network to check the status of internet availability" android:textSize="20sp" /> <Button android:id="@+id/checkstatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="Check Connection" android:textColor="@color/colorPrimaryDark" android:textSize="20sp" /> </LinearLayout> |
2.src/MainActivity.java :-checking internet connection in MainActivity.java screen.
MainActivity.java
package com.example.amitrawat.connectiondetector;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ConnectionDetector cd;
Button check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
check = (Button) findViewById(R.id.checkstatus);
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cd.hasInternetConnection()) {
Toast.makeText(getApplicationContext(),
"Connected To Internet", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Sorry Not Connected To Internet", Toast.LENGTH_LONG).show();
}
}
});
}
}
|