activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="20dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.java
package com.androidian.networkchecking;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.androidian.networkchecking.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
ConnectionDetector connectionDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(
MainActivity.this,R.layout.activity_main);
connectionDetector=new ConnectionDetector();
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(connectionDetector.isConnect(MainActivity.this)){
Toast.makeText(MainActivity.this, "Net is there", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "No Net", Toast.LENGTH_SHORT).show();
}
}
});
}
}
ConnectionDetector.java
package com.androidian.networkchecking;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
public boolean isConnect(Context context){
ConnectivityManager connectivityManager=
(ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager!=null){
NetworkInfo[] infos=connectivityManager.getAllNetworkInfo();
if(infos!=null)
for(int i=0;i<infos.length;i++)
if(infos[i].getState()==NetworkInfo.State.CONNECTED){
return true;
}
}
return false;
}
}
AndoidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Networkchecking"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>