7

在我的应用程序中,我正在检查用户设备上是否启用了 GPS,如果没有,我想将他发送到设置让他打开它。

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, LocationHelper.LOCATION_SETTINGS_REQUEST_CODE);

用户关闭设置屏幕后,我会在onActivityResult().

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == LocationHelper.LOCATION_SETTINGS_REQUEST_CODE) {
        LogUtils.d("onActivityResult from settings");
        fetchCurrentLocation();
    }
}

但是,onActivityResult()不会调用。我做错了什么还是这种方法通常不起作用?提前致谢。

4

2 回答 2

6

lauch the setting intent :

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

and fetch the current location in onResume method :

public void onResume(){
    super.onResume();
    if(isGPSEnabled){
         fetchCurrentLocation();
}
}

after backing from setting screen , your onResume method will be call and here you can fetch your location.

于 2013-03-04T13:57:55.967 回答
-2

您是否在清单中定义了 ACCESS_FINE_LOCATION 权限?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
于 2013-03-04T11:07:03.267 回答