繁体   English   中英

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

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

我需要获得用户的位置。 我明白了

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

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

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

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

如果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);
    }

实现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);
}
 }

您可以使用

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

检查gps后。

完整的来源可以在这里找到,或者您可以参考SO ANSWER

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM