简体   繁体   English

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

[英]How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER?

I need to get location of user. 我需要获得用户的位置。 I put in manifest 我明白了

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

How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER ? 如何提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER?

You can start an options intent for the location settings. 您可以启动位置设置的选项意图。

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

LOCATION_MODE will be LOCATION_MODE_OFF if GPS is off, which will return value 0. 如果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);
    }

Implement LocationListner : so 实现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();
}
}     

Show dialog to open Settings: 显示对话框以打开设置:

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();
}

In your OnCreate Method call : 在您的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);

And ovveride onActivityResult method to test if the user activated correctly gps 并且使用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);
}
 }

you can use 您可以使用

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

after checking gps. 检查gps后。

complete source can be found HERE or you can refer to SO ANSWER 完整的来源可以在这里找到,或者您可以参考SO ANSWER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER 如何从GPS_PROVIDER自动更改为NETWORK_PROVIDER - How to change automatically from GPS_PROVIDER to NETWORK_PROVIDER 如何检查Xamarin表单中的network_provider和gps_provider? - How to check for network_provider and gps_provider in Xamarin forms? 使用OnStatusChanged从GPS_PROVIDER切换到NETWORK_PROVIDER? - Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER? getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL - getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent NETWORK_PROVIDER和GPS_PROVIDER在片段中返回null - NETWORK_PROVIDER and GPS_PROVIDER returns null in fragments 使用GPS_PROVIDER和NETWORK_PROVIDER获取当前位置的问题 - Issue in getting Current Location with GPS_PROVIDER and NETWORK_PROVIDER 在android中,GPS_PROVIDER有效,但NETWORK_PROVIDER不起作用 - in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not Android:locationManager GPS_provider VS Network_provider - Android: locationManager GPS_provider VS Network_provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM