Home > Software engineering >  When the application is open, I want to show an instant alert dialog when the internet is closed and
When the application is open, I want to show an instant alert dialog when the internet is closed and

Time:03-01

The codes are like this, but when I turn off the internet while the application is open, the alert dialog is not displayed.

I want an alert dialog when navigating the app when not connected to the internet or not connected to the internet. I just checked the app, but the warning dialog is not displayed when I turn off the internet while the app is open.

I assigned a boolean value to isConnect, but I need to check it instantly. It will be checked while the application is being planned, but I want the internet as a meeting with me..

how can I do that??

thanks..

public boolean isConnected() {
        connected = false;
        try {
            ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo nInfo = cm.getActiveNetworkInfo();
            connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnected();
            return connected;
        } catch (Exception e) {
            Log.e("Connectivity Exception", e.getMessage());
        }
        return connected;
    }
    public void networkController()  {

        ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nInfo = cm.getActiveNetworkInfo();
       connected = nInfo != null && nInfo.isAvailable() && nInfo.isConnectedOrConnecting();
        // Check for Internet Connection
        if (isConnected()) {
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(Webview.this);
            builder.setTitle("Hata");
            builder.setMessage("İnternet Bağlantınızı Kontrol Edin");
            builder.setPositiveButton("Tekrar Dene", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(Webview.this,Webview.class);
                    startActivity(intent);

                }
            });
            builder.setNegativeButton("Çıkış", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();

                }
            });

            builder.show();
         }
    }  ```

CodePudding user response:

You can make a No internet layout and use the window manager to call that layout as a dialog box.


ConnectivityManager connectivityManager = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isConnected() || !networkInfo.isAvailable()){
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView((R.layout.no_internet_dialog_id));
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations =
    android.R.style.Animation_Dialog;
Button btnretry = dialog.findViewById(R.id.retry_button_id);
btnretry.setOnClickListener(v -> recreate());

dialog.show();
webview.setVisibility(View.GONE);

}

CodePudding user response:

in oncreate method

if(!isNetworkAvailable())
{
ShowAlertDialogDone("Poor or no internet connection")
}

check internet connectivity


    public boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
   

Alert dialog function

private void ShowAlertDialogDone(String stMessage) {
        Dialog dialog = new Dialog(getContext());
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        View view  = getLayoutInflater().inflate(R.layout.alert_dialog, null);
        dialog.setContentView(view);

        TextView Message,btnAllow;
        Message=(TextView)view.findViewById(R.id.tvMessage);
        btnAllow=(TextView)view.findViewById(R.id.btnAllow);

        Message.setText(stMessage);
        btnAllow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            
                dialog.dismiss();
            }
        });

        dialog.show();
    }

alert_dialog (Layout xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/_250sdp"
    android:layout_height="@dimen/_190sdp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/fingertouchbackground"
    android:layout_gravity="center">

    <ImageView
        android:id="@ id/imageView16"
        android:layout_width="@dimen/_50sdp"
        android:layout_height="@dimen/_50sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:src="@drawable/ic_baseline_error_outline_24"
        app:layout_constraintBottom_toTopOf="@ id/card_status"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/card_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/alert_notification"
        android:textColor="@color/black"
        android:textSize="@dimen/_20sdp"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@ id/textView71"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/imageView16" />

    <TextView
        android:id="@ id/textView71"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_1sdp"
        android:background="@color/devider"
        android:layout_marginLeft="@dimen/_30sdp"
        android:layout_marginRight="@dimen/_30sdp"
        app:layout_constraintBottom_toTopOf="@ id/tvMessage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/card_status" />


    <TextView
        android:id="@ id/tvMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingLeft="@dimen/_15sdp"
        android:paddingRight="@dimen/_15sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:text="@string/internt_problem"
        android:textColor="@color/color_gray"
        android:textSize="@dimen/_11sdp"
        android:fontFamily="@font/nunito_semibold"
        app:layout_constraintBottom_toTopOf="@ id/constraintLayoutButtons"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/textView71" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/constraintLayoutButtons"
        android:layout_width="@dimen/_240sdp"
        android:layout_height="@dimen/_40sdp"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_15sdp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/tvMessage">

        <TextView
            android:id="@ id/btnAllow"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_30sdp"
            android:background="@drawable/button_background"
            android:fontFamily="@font/nunito_regular"
            android:gravity="center"
            android:text="@string/ok"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="@dimen/_15sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@ id/constraintLayoutButtons" />
    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
  • Related