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

时间:2009-06-26 23:01:34

标签: java android gps android-1.5-cupcake

我目前正在Android中编写一款可与GPS配合使用的应用。目前我能够确定GPS是否已启用。我的问题是,如果禁用,我想在应用启动时启用GPS。我该怎么做这个程序化的?

7 个答案:

答案 0 :(得分:51)

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

答案 1 :(得分:15)

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

答案 2 :(得分:6)

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

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

答案 3 :(得分:3)

您可以使用以下内容:

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

但只有拥有系统签名保护级别才能生效。所以你需要煮自己的Image来实际使用它:/

答案 4 :(得分:1)

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

答案 5 :(得分:0)

您应该使用Play服务中的Location Settings Dialog,只需点击一下即可提示用户启用位置服务。

答案 6 :(得分:-5)

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

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