Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

前端 未结 7 2171
臣服心动
臣服心动 2020-11-29 20:52

I am using this given below code to get locations:

public Location getLocation() {
        try {
            mLocationManager = (LocationManager) context.get         


        
相关标签:
7条回答
  • 2020-11-29 21:20

    Main Class:

    public class AndroidLocationActivity extends Activity {
    
    Button btnGPSShowLocation;
    Button btnNWShowLocation;
    
    AppLocationService appLocationService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appLocationService = new AppLocationService(
                AndroidLocationActivity.this);
    
        btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);
        btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
    
                Location gpsLocation = appLocationService
                        .getLocation(LocationManager.GPS_PROVIDER);
    
                if (gpsLocation != null) {
                    double latitude = gpsLocation.getLatitude();
                    double longitude = gpsLocation.getLongitude();
                    Toast.makeText(
                            getApplicationContext(),
                            "Mobile Location (GPS): \nLatitude: " + latitude
                                    + "\nLongitude: " + longitude,
                            Toast.LENGTH_LONG).show();
                } else {
                    showSettingsAlert("GPS");
                }
    
            }
        });
    
        btnNWShowLocation = (Button) findViewById(R.id.btnNWShowLocation);
        btnNWShowLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
    
                Location nwLocation = appLocationService
                        .getLocation(LocationManager.NETWORK_PROVIDER);
    
                if (nwLocation != null) {
                    double latitude = nwLocation.getLatitude();
                    double longitude = nwLocation.getLongitude();
                    Toast.makeText(
                            getApplicationContext(),
                            "Mobile Location (NW): \nLatitude: " + latitude
                                    + "\nLongitude: " + longitude,
                            Toast.LENGTH_LONG).show();
    
                } else {
                    showSettingsAlert("NETWORK");
                }
    
            }
        });
    
    }
    
    public void showSettingsAlert(String provider) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                AndroidLocationActivity.this);
    
        alertDialog.setTitle(provider + " SETTINGS");
    
        alertDialog.setMessage(provider
                + " is not enabled! Want to go to settings menu?");
    
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        AndroidLocationActivity.this.startActivity(intent);
                    }
                });
    
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
    
        alertDialog.show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    
    }
    
    }
    

    Next Class:

    public class AppLocationService extends Service implements LocationListener {
    
    protected LocationManager locationManager;
    Location location;
    
    private static final long MIN_DISTANCE_FOR_UPDATE = 10;
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
    
    public AppLocationService(Context context) {
        locationManager = (LocationManager) context
                .getSystemService(LOCATION_SERVICE);
    }
    
    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }
    
    @Override
    public void onLocationChanged(Location location) {
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    }
    

    Don't forget to add in your manifest.

    <!-- to get location using GPS -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <!-- to get location using NetworkProvider -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
提交回复
热议问题