Java 如何在 Android Cupcake 中以编程方式启用 GPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1051649/
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-11 22:47:23  来源:igfitidea点击:

How to programmatically enable GPS in Android Cupcake

javaandroidgpsandroid-1.5-cupcake

提问by Chiwai Chan

I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly?

我目前正在 Android 中编写一个与 GPS 配合使用的应用程序。目前我可以确定是否启用了 GPS。我的问题是,如果 GPS 被禁用,我想在应用程序启动时启用 GPS。我怎样才能以编程方式做到这一点?

采纳答案by CommonsWare

You can't, starting with Android 1.5. The most you can do is pop open the activity to allow the user to toggle it on/off. Use the action held in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGSto craft an Intent to open this activity.

你不能,从 Android 1.5 开始。您最多能做的就是弹出打开活动以允许用户打开/关闭它。使用所持有的行动android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS来制作一个意图来打开这个活动。

回答by TS.xy

if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
    startActivity(myIntent);
}

回答by Leticia

if your question is at the user level of android these properties are located in: "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites".

如果您的问题是在 android 的用户级别,这些属性位于:"Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites"

But at the developer can use the class "android.provider.Settings.Secure"with the appropriate permissions.

但是在开发人员可以使用"android.provider.Settings.Secure"具有适当权限的类。

回答by icyerasor

You might use the following:

您可能会使用以下内容:

try {
  Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
} catch (Exception e) {
  logger.log(Log.ERROR, e, e.getMessage());
}

but it will only work if you have system signature protection level. So you need to cook your own Image to actually use it :/

但它只有在您具有系统签名保护级别时才有效。所以你需要烹饪自己的图像才能实际使用它:/

回答by Dwivedi Ji

This method code can be help for you

此方法代码可以为您提供帮助

private void turnGPSOnOff(){
  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(!provider.contains("gps")){
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
    //Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}

回答by Greg Ennis

You should use the Location Settings Dialogin Play Services that prompts the user to enable location services (if necessary) with just one click.

您应该使用Play 服务中的位置设置对话框,提示用户只需单击一下即可启用位置服务(如有必要)。

回答by yubaraj poudel

First check whether the location service is already on or not ??

首先检查定位服务是否已经开启??

Checking location service is enabled or not

检查位置服务是否启用

public boolean isLocationServiceEnabled(){
    LocationManager locationManager = null;
    boolean gps_enabled= false,network_enabled = false;

    if(locationManager ==null)
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    try{
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch(Exception ex){
        //do nothing...
    }

    try{
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch(Exception ex){
        //do nothing...
    }

    return gps_enabled || network_enabled;

}

Then finally to open if location service in turned off previously

如果之前关闭定位服务,最后打开

  if (isLocationServiceEnabled())) {
          //DO what you need...
     } else {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Seems Like location service is off,   Enable this to show map")
      .setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
                             Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivity(intent);
                                }
                            }).setNegativeButton("NO THANKS", null).create().show();
                }