为结果启动设置活动

时间:2013-03-04 10:47:41

标签: android android-intent android-location

在我的应用程序中,我正在检查用户设备上是否启用了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()不会被调用。我做错了什么或这种做法一般不起作用?提前谢谢。

2 个答案:

答案 0 :(得分:5)

设定意图:

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

并在onResume方法中获取当前位置:

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

从设置屏幕退回后,您的onResume方法将被调用,您可以在此处获取您的位置。

答案 1 :(得分:-2)

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

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