Adam Duong
Adam Duong

Reputation: 95

How can I open location setting by flutter using android_intent

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();

Upvotes: 7

Views: 4901

Answers (3)

IvanPavliuk
IvanPavliuk

Reputation: 1790

For both iOS and Android platforms, you can use the app_settings plugin - https://pub.dev/packages/app_settings

Upvotes: 1

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

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');
}

Upvotes: 3

diegoveloper
diegoveloper

Reputation: 103381

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

Upvotes: 11

Related Questions