Android gps programmatically turning on and turning off

前端 未结 3 1170
后悔当初
后悔当初 2020-12-30 18:05

i have written a simple gps code , which is turning on and turning off the gps programmatically in some device(say moto g) but some older version or Samsung duos and some o

相关标签:
3条回答
  • 2020-12-30 18:49

    First of all it depends if you catch the geolocation by Google Play Services or if you do it the "old" way. I would not suggest to do something behind the user. A common way is to check if the GPS is on and at best direct the user to the system settings by using a 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 Stackoverflow

    0 讨论(0)
  • 2020-12-30 18:56

    Please change it as per your requirement.

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

    Stop GPS and Get Lat Long

    this code for stop 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);
         }
    

    Get Lat Long

    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);
            }       
        }
    }
    
    0 讨论(0)
  • 2020-12-30 19:04
    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题