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

发布于 2024-10-26 04:19:54 字数 105 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

深府石板幽径 2024-11-02 04:20:09

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

检查位置服务是否启用

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;

}

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

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

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();
                }
我很OK 2024-11-02 04:20:09

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

許願樹丅啲祈禱 2024-11-02 04:20:09

如果您的问题是在 android 用户级别,这些属性位于:“设置 -> 位置 -> 使用无线网络” -> “设置 -> 位置 -> 使用 GPS 卫星”

但开发人员可以使用具有适当权限的类“android.provider.Settings.Secure”

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".

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

夏末 2024-11-02 04:20:08

从 Android 1.5 开始,您不能这样做。您最多可以做的是弹出打开活动以允许用户打开/关闭它。使用 android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS 中保存的操作来创建一个 Intent 以打开此 Activity。

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_SETTINGS to craft an Intent to open this activity.

帅气称霸 2024-11-02 04:20:08
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
    startActivity(myIntent);
}
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
    startActivity(myIntent);
}
忱杏 2024-11-02 04:20:08

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

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

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();
  }
}
一张白纸 2024-11-02 04:20:08

您可以使用以下内容:

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

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

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 :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文