Last active
August 29, 2015 14:04
-
-
Save mansueli/170381bce400bc221151 to your computer and use it in GitHub Desktop.
Posicao do GPS - Android (http://pt.stackoverflow.com/questions/27121/pegar-posi%C3%A7%C3%A3o-do-celular-gps)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.rdc" | |
android:versionCode="1" | |
android:versionName="1.0"> | |
<uses-sdk android:minSdkVersion="10" /> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> | |
</uses-permission> | |
<application | |
android:icon="@drawable/icon" | |
android:label="@string/app_name"> | |
<activity | |
android:name=".GetCurrentLocation" | |
android:label="@string/app_name"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rdc; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Locale; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.ContentResolver; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.ActivityInfo; | |
import android.location.Address; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.provider.Settings; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ProgressBar; | |
import android.widget.Toast; | |
public class CidadePeloGPS extends Activity | |
implements OnClickListener { | |
private LocationManager gerenciadordeLugar=null; | |
private LocationListener lugarListener=null; | |
private Button botaoPegaLugar = null; | |
private EditText editarLugar = null; | |
private ProgressBar pb =null; | |
private static final String TAG = "Debug"; | |
private Boolean flag = false; | |
@Override | |
public void onCreate(Bundle instanciaSalva) { | |
super.onCreate(instanciaSalva); | |
setContentView(R.layout.main); | |
setRequestedOrientation(ActivityInfo | |
.SCREEN_ORIENTATION_PORTRAIT); | |
pb = (ProgressBar) findViewById(R.id.progressBar1); | |
pb.setVisibility(View.INVISIBLE); | |
editarLugar = (EditText) findViewById(R.id.editarLugar); | |
botaoPegaLugar = (Button) findViewById(R.id.btnLocation); | |
botaoPegaLugar.setOnClickListener(this); | |
gerenciadordeLugar = (LocationManager) | |
getSystemService(Context.LOCATION_SERVICE); | |
} | |
@Override | |
public void onClick(View v) { | |
flag = displayGpsStatus(); | |
if (flag) { | |
Log.v(TAG, "onClick"); | |
editarLugar.setText("Por favor, mova o seu aparelho para"+ | |
" ver as alteracoes nas coordenadas."+"\nAguarde.."); | |
pb.setVisibility(View.VISIBLE); | |
lugarListener = new MeuListener(); | |
gerenciadordeLugar.requestLocationUpdates(LocationManager | |
.GPS_PROVIDER, 5000, 10,lugarListener); | |
} else { | |
alertbox("Gps Status!!", "O seu GPS esta desabilitado"); | |
} | |
} | |
private Boolean displayGpsStatus() { | |
ContentResolver contentResolver = getBaseContext() | |
.getContentResolver(); | |
boolean gpsStatus = Settings.Secure | |
.isLocationProviderEnabled(contentResolver, | |
LocationManager.GPS_PROVIDER); | |
if (gpsStatus) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
protected void alertbox(String title, String msg) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage("O seu GPS esta desabilitado") | |
.setCancelable(false) | |
.setTitle("** GPS Status **") | |
.setPositiveButton("GPS habilita", | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
Intent meuIntent = new Intent( | |
Settings.ACTION_SECURITY_SETTINGS); | |
startActivity(meuIntent); | |
dialog.cancel(); | |
} | |
}) | |
.setNegativeButton("Cancelar", | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
dialog.cancel(); | |
} | |
}); | |
AlertDialog alert = builder.create(); | |
alert.show(); | |
} | |
/*----------Listener para as coordenadas ------------- */ | |
private class MeuListener implements LocationListener { | |
@Override | |
public void onLocationChanged(Location loc) { | |
editarLugar.setText(""); | |
pb.setVisibility(View.INVISIBLE); | |
Toast.makeText(getBaseContext(),"Localizacao mudou : Lat: " + | |
loc.getLatitude()+ " Lng: " + loc.getLongitude(), | |
Toast.LENGTH_SHORT).show(); | |
String longitude = "Longitude: " +loc.getLongitude(); | |
Log.v(TAG, longitude); | |
String latitude = "Latitude: " +loc.getLatitude(); | |
Log.v(TAG, latitude); | |
/*---------- cidade a partir das coordenadas ------------- */ | |
String nomeCidade=null; | |
Geocoder gcd = new Geocoder(getBaseContext(), | |
Locale.getDefault()); | |
List<Address> enderecos; | |
try { | |
enderecos = gcd.getFromLocation(loc.getLatitude(), loc | |
.getLongitude(), 1); | |
if (enderecos.size() > 0) | |
System.out.println(enderecos.get(0).getLocality()); | |
nomeCidade=enderecos.get(0).getLocality(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
String s = longitude+"\n"+latitude + | |
"\n\nA Cidade atual eh: "+nomeCidade; | |
editarLugar.setText(s); | |
} | |
@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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:weightSum="1"> | |
<TextView | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="Pega a localizacao e nome da Cidade" | |
android:layout_weight="0.20" | |
android:gravity="center" | |
android:textSize="20sp" /> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.33" | |
android:id="@+id/editarLugar" | |
android:editable="false"> | |
<requestFocus></requestFocus> | |
</EditText> | |
<LinearLayout | |
android:id="@+id/layButtonH" | |
android:layout_height="wrap_content" | |
android:layout_width="fill_parent" | |
android:gravity="center" | |
android:layout_weight="0.15"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Pegar localizacao" | |
android:id="@+id/botaoPegaLugar"></Button> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/layloadingH" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.20" | |
android:layout_width="fill_parent" | |
android:gravity="center"> | |
<ProgressBar | |
android:layout_width="wrap_content" | |
android:id="@+id/progressBar1" | |
android:layout_height="wrap_content"></ProgressBar> | |
</LinearLayout> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment