Created
February 20, 2016 03:51
-
-
Save NiltonMorais/aa85ab1eb0f5c479ed31 to your computer and use it in GitHub Desktop.
Latitude e Longitude não funcionam
This file contains hidden or 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 br.uesc.nilton.focoapi14; | |
| import android.Manifest; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.graphics.Bitmap; | |
| import android.location.Location; | |
| import android.location.LocationListener; | |
| import android.location.LocationManager; | |
| import android.provider.Settings; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v4.content.ContextCompat; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.ImageView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| public class MainActivity extends AppCompatActivity implements LocationListener { | |
| private LocationManager locationManager; | |
| private boolean allowNetwork; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| allowNetwork = true; | |
| locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
| if((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { | |
| Toast.makeText(this, "A aplicação não tem permição para usar o GPS", Toast.LENGTH_LONG).show(); | |
| } | |
| else{ | |
| if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { | |
| Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
| Toast.makeText(this, "Ative o GPS para continuar", Toast.LENGTH_LONG).show(); | |
| startActivity(intent); | |
| } else { | |
| locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); | |
| locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); | |
| } | |
| } | |
| } | |
| @Override | |
| protected void onPause() { | |
| super.onPause(); | |
| if((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { | |
| } | |
| else{ | |
| locationManager.removeUpdates(this); | |
| } | |
| } | |
| public void tirarFoto(View view) { | |
| Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); | |
| startActivityForResult(intent, 0); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| if (data != null) { | |
| Bundle bundle = data.getExtras(); | |
| if (bundle != null) { | |
| Bitmap img = (Bitmap) bundle.get("data"); | |
| ImageView iv = (ImageView) findViewById(R.id.imageView); | |
| iv.setImageBitmap(img); | |
| } | |
| } | |
| } | |
| @Override | |
| public void onLocationChanged(Location location) { | |
| // EVENTO CHAMADO QUANDO O POSICIONAMENTO É ALTERADO | |
| Log.i("nilton", "Localizacao ok"); | |
| Toast.makeText(this, "onLocationChanged", Toast.LENGTH_LONG).show(); | |
| TextView txtLat = (TextView) findViewById(R.id.txtLatitude); | |
| TextView txtLng = (TextView) findViewById(R.id.txtLongitude); | |
| if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) { | |
| allowNetwork = false; | |
| } | |
| //if (allowNetwork || location.getProvider().equals(LocationManager.GPS_PROVIDER)){ | |
| Toast.makeText(this, "Localização ok", Toast.LENGTH_LONG).show(); | |
| txtLat.setText("Latitude: " + location.getLatitude()); | |
| txtLng.setText("Longitude: " + location.getLongitude()); | |
| //} | |
| } | |
| @Override | |
| public void onStatusChanged(String provider, int status, Bundle extras) { | |
| // EVENTO DISPARADO QUANDO O STATUS/SINAL DO GPS É ALTERADO | |
| Toast.makeText(this, "onStatusChanged", Toast.LENGTH_LONG).show(); | |
| } | |
| @Override | |
| public void onProviderEnabled(String provider) { | |
| // EVENTO DISPARADO QUANDO GPS É ATIVADO | |
| Log.i("log", "GPS ATIVADO"); | |
| Toast.makeText(this, "onProviderEnabled", Toast.LENGTH_LONG).show(); | |
| } | |
| @Override | |
| public void onProviderDisabled(String provider) { | |
| // EVENTO DISPARADO QUANDO GPS É DESATIVADO | |
| Log.i("log", "GPS DESATIVADO"); | |
| Toast.makeText(this, "onProviderDisabled", Toast.LENGTH_LONG).show(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment