Android gps 以编程方式打开和关闭

Android gps programmatically turning on and turning off

我写了一个简单的 gps 代码,它在某些设备(比如 moto g)中以编程方式打开和关闭 gps,但一些旧版本或 Samsung duos 和一些其他设备它不以编程方式打开 gps。 请帮我解决这个问题,

如果gps开启成功我会得到纬度和经度,所以我会用它

提前致谢

You have to decrease your API level from Manifest File

首先,这取决于您是通过 Google Play Services 还是通过 "old" 方式获取地理位置。我不建议在用户背后做一些事情。一种常见的方法是检查 GPS 是否打开,最多使用 PreferenceScreen. Also inform the user with a DialogPreference or a toast. Otherwise if you want to do it the dirty way, you can find an answer on Whosebug

将用户引导至系统设置

请根据您的要求进行更改。

关闭 GPS

    private void turnGPSOn() 
{

    String provider = android.provider.Settings.Secure.getString(getContentResolver(),android.provider.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);
    }
}

停止 GPS 并获取经纬度

此代码用于停止 gps

LocationUtil locationUtil = new LocationUtil(getApplicationContext());
                        locationUtil.stopUsingGPS();
 if (statusOfGPS) {

         Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
         intent.putExtra("enabled", false);
         sendBroadcast(intent);
    }

     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
     if(provider.contains("gps"))
     { //if gps is enabled
         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);
     }

获取经纬度

public class LocationUtil implements LocationListener 
{
private Context context;
private LocationManager locationManager;
Location ToPassLocation = null;
// chetan changes
private String provider;

// changes

public LocationUtil(Context context) 
{
    this.context = context;
    Log.d("Location", "Object created");

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, this);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, this);
}

public Location getLatLongLast() 
{
    // chetan changes
    if (ToPassLocation == null) 
    {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);

        if (provider.isEmpty()) 
        {   
            Log.e("Location_Util", "OLD Provider:- NETWORK_Provider");
            provider = LocationManager.NETWORK_PROVIDER;
            onProviderEnabled(provider);
            Location location = locationManager.getLastKnownLocation(provider);
            onProviderEnabled(provider);
            return location;
        }
        else if (provider.isEmpty()) 
        {
            Log.e("Location_Util", "OLD Provider:- GPS_Provider");
            provider = LocationManager.GPS_PROVIDER;
            onProviderEnabled(provider);
            Location location = locationManager.getLastKnownLocation(provider);
            onProviderEnabled(provider);
            return location;
        }
        else 
        {
            Log.e("Location_Util", "OLD Provider:- PASSIVE_Provider");
            provider = LocationManager.PASSIVE_PROVIDER;
            onProviderEnabled(provider);
            Location location = locationManager.getLastKnownLocation(provider);
            onProviderEnabled(provider);
            return location;
        }
    } 
    else 
    {
        Log.e("Location_Util", "NEW Provider:- Will get while calling get provider");
        return ToPassLocation;
    }
} 

  //    public Location getLatLongLastNew()
 // { 
//      return ToPassLocation;
//  } 
//  

@Override
public void onLocationChanged(Location location) 
{
    ToPassLocation = location;
    System.err.println("Location New:-"+ ToPassLocation);
    System.err.println("Latitude__:-"+location.getLatitude()+"-:");
    System.err.println("Longitude_:-"+location.getLongitude()+"-:");
    Log.e("Location_Util",  "Provider__:-"+location.getProvider());
    location.getLatitude();
    location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) 
{
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) 
{}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
{
    // TODO Auto-generated method stub
}

  public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(this);
        }       
    }
}
public void turnGPSOn() {
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    this.sendBroadcast(intent);

    String provider = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if (!provider.contains("gps")) { //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.setData(Uri.parse("3"));
        this.sendBroadcast(poke);
    }
}

// automatic turn off the gps
public void turnGPSOff() {
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    this.sendBroadcast(intent);

    String provider = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if (provider.contains("gps")) { //if gps is enabled
        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"));
        this.sendBroadcast(poke);
    }
}