Ini adalah contoh bagaimana untuk mengetahui status jaringan internet di android menggunakan ConnectivityManager dan NetworkInfo
Langkah 1 - Buka android studio dan buat project baru
Langkah 2 − pada file manifest.xml android.permission.ACCESS_NETWORK_STATE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.karnadi.myapplication">
<uses-permission android:name="android.permission.ACCESS_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/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>
Langkah 3 - Tulis kode ini ke dalam file main_activity.java
package id.karnadi.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCheck = findViewById(R.id.buttonCheck);
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
check();
}
});
}
private void check(){
String status = "NO CONNECTION";
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
if(networkInfo.getType() == connectivityManager.TYPE_WIFI){
status = "WIFI CONNECTED";
} else if (networkInfo.getType() == connectivityManager.TYPE_MOBILE){
status = "MOBILE CONNECTED";
}
}
Toast.makeText(this,status,Toast.LENGTH_LONG).show();
}
}
Langkah 4 - pada file layout activity_main.xml tulis kode seperti ini
<?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">
<Button
android:id="@+id/buttonCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sebagai referensi
- https://developer.android.com/training/basics/network-ops/managing#java
- https://developer.android.com/training/monitoring-device-state/connectivity-status-type#java
Artikel Lain
- Bagaimana cara memasukan item baru di urutan pertama sebuah array
- Kode Status HTTP
- Cara Melamar Kerja lewat Email
- Cara Menagatasi error Gradle sync failed: No cached version of org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61 available for offline mode
Daily Popular
- Firmware Xperia Z5 (E6653) - Generic GLOBAL
- Bismillah
- Flash Sony Experia Z3+ (E6553)
- Kode Status HTTP
KOMENTAR