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

时间:2015-02-10 06:56:02

标签: android gps

我已经编写了一个简单的gps代码,它在一些设备(例如moto g)中以编程方式打开和关闭gps,但是一些旧版本或三星二人和其他一些设备没有以编程方式打开gps。 请帮我解决这个问题,

如果gps成功打开,我将获得纬度和经度,所以我将使用它

并提前致谢

  

您必须从清单文件中降低API级别

3 个答案:

答案 0 :(得分:5)

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

转向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);
        }       
    }
}

答案 1 :(得分:0)

首先,这取决于您是通过Google Play服务捕获地理定位还是以“旧”方式执行此操作。我不建议做一些用户背后的事情。一种常见的方法是检查GPS是否打开,并且最多使用PreferenceScreen将用户引导至系统设置。同时通过DialogPreference或吐司通知用户。否则,如果你想以肮脏的方式去做,你可以在Stackoverflow

找到答案

答案 2 :(得分:0)

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