【问题标题】:How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER?如何提示用户启用 GPS_PROVIDER 和/或 NETWORK_PROVIDER?
【发布时间】:2011-10-10 13:27:50
【问题描述】:

我需要获取用户的位置。我把清单放进去

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

如何提示用户启用 GPS_PROVIDER 和/或 NETWORK_PROVIDER ?

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以为位置设置启动选项意图。

    Intent gpsOptionsIntent = new Intent(  
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
    startActivity(gpsOptionsIntent);
    

    【讨论】:

    • 但这会导致设置屏幕出现循环,无论何时返回它仍会进入设置屏幕
    • 为什么会导致循环?
    • 我认为这取决于你把这段代码放在哪里。如果您在 App 恢复时启动 Activity,您可能会陷入循环。但不是代码本身造成的。
    • 你不应该把这个放在简历上!
    【解决方案2】:

    如果 GPS 关闭,LOCATION_MODE 将为 LOCATION_MODE_OFF,返回值 0。

    int off = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE);
        if(off==0){
            Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(onGPS);
        }
    

    【讨论】:

    • 谢谢!现在 LOCATION_MODE 已被弃用,还有其他解决方案吗?
    【解决方案3】:

    实现 LocationListner :所以

     @Override
     public void onLocationChanged(Location location) {
    
     }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    
    }
    
     @Override
     public void onProviderEnabled(String provider) {
    
    }
    
    @Override
    public void onProviderDisabled(String provider)
    {
    if (provider.equals(LocationManager.GPS_PROVIDER))
    {
        showGPSDiabledDialog();
    }
    }     
    

    显示对话框以打开设置:

    public void showGPSDiabledDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("GPS Disabled");
    builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
    builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
        }
    }).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
    
        }
    });
    mGPSDialog = builder.create();
    mGPSDialog.show();
    }
    

    在您的 OnCreate 方法调用中:

     LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        return;
    }
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    

    和 ovveride onActivityResult 方法来测试用户是否正确激活了 gps

        @Override
    
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
    if (requestCode == GPS_ENABLE_REQUEST)
    {
        if (mLocationManager == null)
        {
            mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        }
    
        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            showGPSDiabledDialog();
        }
    }
    else
    {
        super.onActivityResult(requestCode, resultCode, data);
    }
     }
    

    【讨论】:

      【解决方案4】:

      你可以使用

      Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
             startActivity(intent);
      

      在检查 gps 之后。

      完整的源码可以在HERE找到或者你可以参考SO ANSWER

      【讨论】:

      • 将 ACTION_LOCATION_SOURCE_SETTINGS 更改为 ACTION_SECURITY_SETTINGS
      • 指向另一个 SO 答案的有用链接!
      猜你喜欢
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多