Internet and Wifi connection on off Android Studio Java | Network Enable or Disable in All Android Versions
Hellow Developers, in this Blog we will see how to enable or disable Internet connection from our Android Aplication. So i also Give and exaple at the bottom and also youcan download this project from Project Store.
Enable or Disable Connection
private void showConnectionDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivityIfNeeded(new Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY), 1002);
} else {
startActivityIfNeeded(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1002);
}
}
private boolean isNetworkAvailable() {
android.net.ConnectivityManager connectivityManager = (android.net.ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1002) {
if(isNetworkAvailable()) {
//use Timer because network need some time for connect successfully
TimerTask t =new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Connection Successful", Toast.LENGTH_SHORT).show();
}
});
}
};
Timer _t = new Timer();
_t.schedule(t, 1000);
} else {
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT).show();
}
}
}
Show uses
showConnectionDialog();
Permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Here I have ginven a demo project in in the below.
MainActivity.java
package com.sketchfamily.internetonoff;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showConnectionDialog();
}
});
}
private void showConnectionDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivityIfNeeded(new Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY), 1002);
} else {
startActivityIfNeeded(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1002);
}
}
private boolean isNetworkAvailable() {
android.net.ConnectivityManager connectivityManager = (android.net.ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1002) {
if(isNetworkAvailable()) {
//use Timer because network need some time for connect successfully
TimerTask t =new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Connection Successful", Toast.LENGTH_SHORT).show();
}
});
}
};
Timer _t = new Timer();
_t.schedule(t, 1000);
} else {
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT).show();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn On Internet"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.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"
package="com.sketchfamily.internetonoff" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.InternetOnOff" >
<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>
How to restore this demo project ?
For restoring this demo project just create a new project in Android Studio. After creating just paste this codes in your project. Thank You.
0 Comments