Ich bin neu bei der Programmierung in Java und auch bei der Programmierung in Android, und ich versuche, meinen Standort zu bekommen, aber ich bekomme einige Probleme.Probleme bei der Ortung auf Android
dies ist der gesamte Code I
public class MiServicio extends Service implements LocationListener{private final Context context;
double latitud;
double longitud;
Location location;
boolean gpsActivo;
TextView texto;
LocationManager locationManager;
public MiServicio() {
super();
this.context = this.getApplicationContext();
}
public MiServicio(Context c) {
super();
this.context = c;
getLocation();
}
public void setView(View v) {
texto = (TextView) v;
texto.setText("Coordenadas: " + latitud + ", " + longitud);
}
public void getLocation() {
try {
locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);
gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ignored) {
}
if (gpsActivo) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitud = location.getLatitude();
longitud = location.getLongitude();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
ich durch das Debuggen geschrieben haben herausgefunden haben, dass es abstürzt, wenn es die wenn Satz in diesem Teil des
Code erreichtif (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);
Also könnte mir jemand erklären, wie man dieses Problem löst?
Grüße.
Auch hier sind die Berechtigungen ich im Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
bearbeiten
These are all the errors I got
und auch eingestellt habe ich mein Telefon benutzen Sie diese App
Welche Fehler bekommen Sie? Zeigen Sie den Logcat – tahsinRupam
Was ist Ihr Emulator? oder Ist die Geräteposition aktiviert? –
Siehe [Diesen Link] (http://stackoverflow.com/questions/37322645/nullpointerexception-when-try-to-check-permissions). Ich hoffe, dies wird Ihnen helfen, Ihr Problem zu lösen. –