我正在尝试创建一个用于阅读位置的类,并且它可以用于我的Android应用程序中的不同活动。但没有任何作用。 我认为问题是当我尝试调用方法刷新活动时。
位置自定义类
public class Localizacion implements LocationListener {
//Constantes
private static final int LOCATION_MIN_TIME = 30 * 1000;
private static final int LOCATION_MIN_DISTANCE = 10;
/*Objetos de Inicialización*/
private Actividad Actividad;
private LocationManager Administrador;
private Location Localizador;
/*Objetos de Respuesta*/
private Double Longitud;
private Double Latitud;
private Double Altitud;
private String Metodo;
public Localizacion(Context Contexto) {
this.Actividad = (Actividad) Contexto;
if (ActivityCompat.checkSelfPermission(Actividad.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Actividad.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
this.Administrador.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, (LocationListener) this);
onLocationChanged(this.Localizador);
}
private void ObtenerMetodo() {
Context ContextoActual = Actividad.getApplicationContext();
if (ActivityCompat.checkSelfPermission(ContextoActual, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ContextoActual, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
this.Localizador = Administrador.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(this.Localizador == null) {
this.Localizador = Administrador.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(this.Localizador == null){
this.Metodo = "RED";
}
else {
this.Metodo = "N/A";
}
}
else {
this.Metodo = "GPS";
}
}
@Override
public void onLocationChanged(Location location) {
if(Localizador != null) {
Longitud = location.getLongitude();
Latitud = location.getLatitude();
Altitud = location.getAltitude();
}
else
{
Longitud = null;
Latitud = null;
Altitud = null;
ObtenerMetodo();
}
}
public Double AltitudActual(){
return Altitud;
}
public Double LatitudActual(){
return Latitud;
}
public Double LongitudActual(){
return Longitud;
}
public String MetodoActual(){
return Metodo;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
ObtenerMetodo();
}
@Override
public void onProviderDisabled(String provider) {
ObtenerMetodo();
}
}
活动自定义类
public abstract class Actividad extends AppCompatActivity {
public Actividad(){
super();
}
public void Actualizar(){
}
}
活动应用
public class ChooseSensor extends Actividad {
Localizacion LocalizacionActual;
FrameLayout Contenido;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_sensor);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Contenido = (FrameLayout) findViewById(android.R.id.content);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_choose_sensor, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Actualizar(){
TextView textView = (TextView) Contenido.findViewById(R.id.texto);
textView.setText("Metodo: "+LocalizacionActual.MetodoActual()+" Lon: "+LocalizacionActual.LongitudActual()+" Lat: "+LocalizacionActual.LatitudActual()+" Alt: "+LocalizacionActual.AltitudActual());
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
LocalizacionActual = new Localizacion(this);
}
}