如何检查Android GPS是否被禁用[重复]

2024-01-18

我有两个文件 MainActivity.java 和 HomeFragment.java,MainActivity 中调用 HomeFragment 中的一个函数,要求用户打开手机上的位置服务。问题是,即使用户已经打开了位置功能,该功能仍然会被调用。有没有办法让我只有在位置功能关闭时才启动 HomeFragment 的功能。

HomeFragment.java

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 = "Enable either GPS or any other location"
            + " service to find current location.  Click OK to go to"
            + " location services settings to let you do so.";


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

    builder.create().show();
}

MainActivity.java

public void showMainView() {
    HomeFragment.displayPromptForEnablingGPS(this);
}

谢谢 :)


你可以使用这样的东西:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    // Call your Alert message
}

这应该够了吧。

要检查位置服务是否已启用,您可以使用类似于以下的代码:

String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders == null || locationProviders.equals("")) {
...
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}

来源: 找到马库斯的帖子here https://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检查Android GPS是否被禁用[重复] 的相关文章

随机推荐