Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open location setting by flutter using android_intent

Tags:

flutter

dart

How can I open location setting as image below by using android_intent or url_launcher?

enter image description here

I tried some ways but it did not work:

Example:

final AndroidIntent intent = new AndroidIntent(
  action: 'action_view',
  package: 'android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS'
);
intent.launch();
like image 203
Adam Duong Avatar asked Oct 19 '18 05:10

Adam Duong


People also ask

How do you open the location in flutter?

Method 1: Location Setting Popup within App To show the location popup within the app, you can use location package in your Project. Add this package by adding the following lines in your pubspec. yaml file. This code will show the following alert dialog within App.

How do you get location permission in flutter?

The Android 11 option to always allow is not presented on the location permission dialog prompt. The user has to enable it manually from the app settings. This should be explained to the user on a separate UI that redirects the user to the app's location settings managed by the operating system.


2 Answers

You can use the android_intent package but the action is incorrect, you have to use this :

    final AndroidIntent intent = new AndroidIntent(
    action: 'android.settings.LOCATION_SOURCE_SETTINGS',);
    intent.launch();

Reference: https://developer.android.com/reference/android/provider/Settings.html#ACTION_LOCATION_SOURCE_SETTINGS

like image 50
diegoveloper Avatar answered Jan 03 '23 19:01

diegoveloper


You also can do it without additional dependencies like android_intent or url_launcher

class MainActivity : FlutterActivity() {
    companion object {
        private const val CHANNEL = "com.myapp/intent"
        const val MAP_METHOD = "settings"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GeneratedPluginRegistrant.registerWith(this)
        MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == MAP_METHOD) {
                startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
                result.success(null)
            } else {
                result.notImplemented()
            }
        }
    }
}

And if dart code:

static const platform = const MethodChannel('com.myapp/intent');

void openSettings() async {
    await platform.invokeMethod('settings');
}
like image 33
Andrey Turkovsky Avatar answered Jan 03 '23 21:01

Andrey Turkovsky