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

Java 如何在Android Cupcake中以编程方式启用GPS,java,android,gps,android-1.5-cupcake,Java,Android,Gps,Android 1.5 Cupcake,我目前正在用Android编写一个应用程序,它可以和GPS一起工作。目前,我能够确定是否启用了GPS。我的问题是,如果GPS在应用程序启动时被禁用,我想启用它。我怎样才能做这个程序呢?你不能,从Android 1.5开始。您所能做的最多就是弹出打开活动,允许用户将其打开/关闭。使用安卓.provider.Settings.action\u LOCATION\u SOURCE\u Settings中的操作来构思打开此活动的意图。如果您的问题是安卓用户级别的,则这些属性位于:“设置->位置->使用无

我目前正在用Android编写一个应用程序,它可以和GPS一起工作。目前,我能够确定是否启用了GPS。我的问题是,如果GPS在应用程序启动时被禁用,我想启用它。我怎样才能做这个程序呢?

你不能,从Android 1.5开始。您所能做的最多就是弹出打开活动,允许用户将其打开/关闭。使用安卓.provider.Settings.action\u LOCATION\u SOURCE\u Settings中的操作来构思打开此活动的意图。

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

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

但是,开发人员可以在适当的权限下使用类“android.provider.Settings.Secure”。

您可以使用以下选项:

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

但只有当您具有系统签名保护级别时,它才会起作用。因此,您需要制作自己的图像以实际使用它://

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

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

您应该使用在用服务,提示用户只需单击一下即可启用位置服务(如有必要)。

首先检查位置服务是否已启用

检查位置服务是否启用

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

取决于纸杯蛋糕的版本。1.5显然是不允许的。为什么会禁用它?为什么不允许开发人员切换此选项?电源控制小部件可以,所以我们也应该能够。你不这么认为吗?因为隐私的原因它被禁用了。如果用户想要关闭GPS,用户应该关闭GPS,句号。这些家伙似乎已经解决了这个问题。不幸的是,apk是模糊的,我无法理解它是如何完成的。URL:@commonware,但实际上可以利用2.2以下的漏洞来实现。我相信您知道详细信息,但对于其他人,请参阅@RichardLeMesurier:至少对于较新版本的Android,这样的安全漏洞已经修复。这段代码没有在Android 2.2上为我编译,因为.isProviderEnabled不是LocationManager上的静态方法。我的工作代码如下(抱歉格式化)LocationManager LocationManager=(LocationManager)getSystemService(LOCATION_服务);如果(!locationManager.isProviderEnabled(locationManager.GPS_PROVIDER)){Intent myIntent=new Intent(Settings.ACTION_LOCATION_SOURCE_Settings);startActivity(myIntent);}是,可以在2.2(sdk 8)之前完成此操作。欲了解更多信息,请参阅这不是答案,而是提问者想要的答案