Java 如何在安卓设备上打开 GPS 设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23040392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:15:05  来源:igfitidea点击:

How to open GPS settings an android device?

javaandroidactionscript-3

提问by Tinko

I am making GPS android app (with air for android) and what I want is if GPS is off I want to open GPS settings on device or switch GPS on but I don't know how. I want to do it in AS3 or open android settings in Java. Thanks for help!

我正在制作 GPS android 应用程序(带有用于 android 的空气),我想要的是如果 GPS 关闭,我想在设备上打开 GPS 设置或打开 GPS,但我不知道如何。我想在 AS3 中执行此操作或在 Java 中打开 android 设置。感谢帮助!

采纳答案by GVillani82

You can simply start an activity with this action:

你可以简单地用这个动作开始一个活动:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

I use it inside a dialog:

我在对话框中使用它:

public static void displayPromptForEnablingGPS(final Activity activity)
    {

        final AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Do you want open GPS setting?";

        builder.setMessage(message)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        activity.startActivity(new Intent(action));
                        d.dismiss();
                    }
            })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        d.cancel();
                    }
            });
        builder.create().show();
    }

回答by Karthik

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