Starting Settings Activity for result

戏子无情 提交于 2020-01-02 03:17:16

问题


In my application I'm checking whether the GPS is enabled on the user's device, and if not I would like to send him to the Settings to let him turn it on.

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

After the user closes Settings screen, I would to perform an action right inside the onActivityResult().

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

However, the onActivityResult() doesn't get called. Am I doing something wrong or this approach doesn't work in general? Thanks in advance.


回答1:


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.




回答2:


Have you defined ACCESS_FINE_LOCATION permission in your manifest?

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


来源:https://stackoverflow.com/questions/15199761/starting-settings-activity-for-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!