0

I want to show alert dialog in my android application if Network or GPS is disable, How it possible .please help me i am new for android

Hafijur Rahman
  • 53
  • 1
  • 1
  • 10

2 Answers2

0

You'll need to use a broadcast receiver for network state changes & gps connectivity state changes, see: Check INTENT internet connection and How can I check the current status of the GPS receiver?

gjsalot
  • 768
  • 7
  • 5
0

The function (Boolean return) to test if GPS is enabled is:

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
if(provider.contains("gps")) {}

To determine if Network is enabled (including dialog):

ConnectivityManager conMgr =  ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null){
    Description.setVisibility(View.INVISIBLE);
    new AlertDialog.Builder(WelcomePage.this)
        .setTitle(getResources().getString(R.string.app_name))
        .setMessage(getResources().getString(R.string.internet_error))
        .setPositiveButton("OK", null).show();
}else{
    dialog = ProgressDialog.show(WelcomePage.this, "", "Loading...", true,false);
    new Welcome_Page().execute();
}

Android - Programmatically check internet connection and display dialog if notConnected