Reputation: 330
I'm trying to use a sample code (http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example) that I found to see how the location services work. My code compiles, however it seems that I can't get past one part in particular when the app runs. Below is the code I am using
package com.example.gpstest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
private EditText editTextShowLocation;
private Button buttonGetLocation;
private ProgressBar progress;
private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
private boolean gps_enabled = false;
private boolean network_enabled = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
progress = (ProgressBar) findViewById(R.id.progressBar1);
progress.setVisibility(View.GONE);
buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
buttonGetLocation.setOnClickListener(this);
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Attention!");
builder.setMessage("Sorry, location is not determined. Please enable location providers");
builder.setPositiveButton("OK", this);
builder.setNeutralButton("Cancel", this);
builder.create().show();
progress.setVisibility(View.GONE);
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
String londitude = "Londitude: " + location.getLongitude();
String latitude = "Latitude: " + location.getLatitude();
String altitiude = "Altitiude: " + location.getAltitude();
String accuracy = "Accuracy: " + location.getAccuracy();
String time = "Time: " + location.getTime();
editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
progress.setVisibility(View.GONE);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEUTRAL){
editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
}else if (which == DialogInterface.BUTTON_POSITIVE) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}
}
The app will not get past this if statement (I keep getting the msg "Sorry, location is not d..." that it kicks out) :
if (!gps_enabled & !network_enabled)
The original statement read:
if (!gps_enabled && !network_enabled)
however I couldn't get this to compile (eclipse didn't like all the 'amps'), so I took my best guess at what it was trying to do, but I think I butchered its meaning.
if anyone could help me get this code to work, or point me in the direction of a different example so I can play with it, that would be very helpful.
edit I did add all the appropriate permmisions: access coarse location, access fine location, and Internet (just for laughs, as I'm almost positive this code doesn't need Internet)
edit changed the '&' in my code to '&&'
edit added permissions manualy and everything is now working (I orignaly used the GUI option):
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Upvotes: 0
Views: 255
Reputation: 1279
I copied your code into a new project, added the two required permissions to the manifest, and used the &&
replacement Jim suggested. It works just fine on my phone.
I think you must have the permissions set incorrectly in the manifest or, probably, your phone doesn't have its GPS properly enabled for your app.
The documentation for isProviderEnabled
states that "If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned." The only exception I know of is that the method always returns false if your app doesn't declare the needed permissions.
If you're on an emulator or if you have your GPS disabled, that would explain it. Try getting your current location in an existing location-based application, like Google Maps.
Upvotes: 1
Reputation: 45025
It looks like you probably need a short-circuiting AND operator &&
, rather than the
bitwise AND operator &
there.
Upvotes: 1