如何使用android_intent通过Flutter打开位置设置

时间:2018-10-19 05:16:21

标签: dart flutter

如何使用android_intent或url_launcher在下面的图像中打开位置设置?

enter image description here

我尝试了一些方法,但是没有用:

示例:

final AndroidIntent intent = new AndroidIntent(
  action: 'action_view',
  package: 'android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS'
);
intent.launch();

3 个答案:

答案 0 :(得分:6)

您可以使用android_intent软件包,但是操作不正确,您必须使用此软件包:

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

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

答案 1 :(得分:2)

您也可以在没有其他依赖项(例如android_intent或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()
        }
    }
}

}

如果是飞镖代码:

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

void openSettings() async {
    await platform.invokeMethod('settings');
}

答案 2 :(得分:1)

对于iOS和Android平台,您都可以使用app_settings插件-https://pub.dev/packages/app_settings