ANDROID Show a dialog when the Internet/GPS loses connectivity or is not connected

后端 未结 3 1040
花落未央
花落未央 2021-01-29 00:02

[EDITED]

I want to show a Splash Screen / dialog when the Internet or GPS is down or not connected so the user can\'t use the app until the connection is good again.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 00:53

    Try below solution

    STEP 1:

    Make a Java Class named NetworkStatus

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    public class NetworkStatus 
    {
        Context context;
    
        public NetworkStatus(Context context) 
        {
            this.context = context;
        }
    
        public boolean isNetworkOnline() 
        {
            boolean status = false;
    
            try
            {
                ConnectivityManager cm = (ConnectivityManager) 
                        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
                NetworkInfo netInfo = cm.getNetworkInfo(0);
    
                if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) 
                {
                    status = true;
                } 
                else 
                {
                    netInfo = cm.getNetworkInfo(1);
    
                    if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) 
                    {
                        status = true;
                    }
                }
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
    
                return false;
            }
    
            return status;
        }
    
    }
    

    STEP 2:

    Make another Class named AlertDialogManager

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.provider.Settings;
    import com.lmkt.wfm.R;
    import com.lmkt.wfm.activities.ActivityMainScreen;
    
    public class AlertDialogManager 
    {
        Context context;
    
        public AlertDialogManager(Context context) 
        {
            this.context = context;
        }
    
        public void showAlertDialog(String title, final String message, final Boolean status) 
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    
            alertDialog.setTitle(title);
    
            alertDialog.setMessage(message);
    
            if(status != null)
            {
                alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
            }
    
            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {   
                    if(message.contains("You do not have Internet Connection"))
                    {
                          ((Activity) context).finish();
                    }
    
                    dialog.dismiss();
                }
            });
    
            alertDialog.show();
        }
    }
    

    STEP 3: Usage from your Splash Activity:

    AlertDialogManager alert;
    
    NetworkStatus ns;
    
    ns = new NetworkStatus(context);
    
    if(!(ns.isNetworkOnline()))
            {
                alert = new AlertDialogManager(context);
    
                alert.showAlertDialog("Internet Connection!", "You do not have Internet Connection. "
                        + "Please connect to the "
                        + "Internet to sync Data From Server...", false);
            }
    

提交回复
热议问题