Skip to content

Instantly share code, notes, and snippets.

@stivio00
Created December 17, 2010 21:54
Show Gist options
  • Save stivio00/745769 to your computer and use it in GitHub Desktop.
Save stivio00/745769 to your computer and use it in GitHub Desktop.
loc at android v2
/**
* @author Stephen Krol (mlea)
*/
package org.mlea;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity2 extends Activity {
private String provider;
private int choice = -1;
private Location lastLocation;
private LocationManager mLocationManager;
private TextView labelView;
private TextView locateView;
private Button showButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
labelView = (TextView) findViewById(R.id.label);
locateView = (TextView) findViewById(R.id.label2);
showButton = (Button) findViewById(R.id.show_button);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
provider = null;
showButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:"+lastLocation.getLatitude()+
","+lastLocation.getLongitude())
);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add("Change Provider");
menu.add("GPS Status");
menu.add("About");
return result;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean result = super.onOptionsItemSelected(item);
CharSequence itemTitle = item.getTitle();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] providersArray =
mLocationManager.getAllProviders().toArray(new String[]{});
if(itemTitle.equals("Change Provider")){
builder.setTitle("Select a Location Provider");
builder.setSingleChoiceItems(
providersArray, choice,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice = which;
provider = providersArray[which];
update();
}
});
}else if(itemTitle.equals("About")){
builder.setTitle("About...");
builder.setMessage("Proyecto MLEA, Test para GPS"
+"\n mlea.wikispot.org");
}else if(itemTitle.equals("GPS Status")){
GpsStatus status = mLocationManager.getGpsStatus(null);
StringBuilder output = new StringBuilder();
output.append("Max numbre of satellites : "+status.getMaxSatellites()+"\n");
output.append("Satelites list:\n ");
for(GpsSatellite sat:status.getSatellites()){
output.append(sat.toString());
}
builder.setMessage(output.toString());
}
AlertDialog options = builder.create();
options.show();
return result;
}
private void update() {
if(!mLocationManager.isProviderEnabled(provider)){
Toast.makeText(this,
"Sorry this Provider :"+provider+" .\n"
+"is not Enable =("
, 2000).show();
labelView.setText("Please select another Provider");
showButton.setVisibility(Button.INVISIBLE);
choice = -1;
return;
}
showButton.setVisibility(Button.VISIBLE);
labelView.setText("The provider : "+provider+" is Selected");
lastLocation = mLocationManager.getLastKnownLocation(provider);
locateView.setText(formatOut(lastLocation));
}
private String formatOut(Location location){
StringBuilder out = new StringBuilder();
Time time = new Time();
time.set(location.getTime());
out.append("Accurancy : "+location.getAccuracy()+"\n");
out.append("Latitude : "+location.getLatitude()+"\n");
out.append("Longitude : "+location.getLongitude()+"\n");
out.append("Time : "+time.format3339(false)+"\n");
return out.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment