【问题标题】:How to show GPS and network alert dialog in whole application if network or GPS disable如果网络或 GPS 禁用,如何在整个应用程序中显示 GPS 和网络警报对话框
【发布时间】:2017-05-31 16:43:59
【问题描述】:

如果网络或 GPS 被禁用,我想在我的 android 应用程序中显示警报对话框,这怎么可能。请帮助我,我是 android 新手

【问题讨论】:

    标签: android android-gps


    【解决方案1】:

    您需要使用广播接收器来更改网络状态和 gps 连接状态,请参阅:Check INTENT internet connectionHow can I check the current status of the GPS receiver?

    【讨论】:

      【解决方案2】:

      测试是否启用 GPS 的函数(布尔返回)是:

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

      确定是否启用网络(包括对话框):

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 2021-03-08
        • 2012-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多