2017-05-03 3 views
0

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 erreicht
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); 

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

+0

Welche Fehler bekommen Sie? Zeigen Sie den Logcat – tahsinRupam

+0

Was ist Ihr Emulator? oder Ist die Geräteposition aktiviert? –

+0

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. –

Antwort

0

Versuchen zu testen Verwenden Sie den folgenden Code in if() Anweisung:

if (context.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; 
    } 
0

bitte an Ihren Code ersetzen für die Überprüfung und Genehmigung Gewährung von unten Code

 int permissionCheck = ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION); 
     if (permissionCheck != PackageManager.PERMISSION_GRANTED) { 
      checkPermission(); 
     } 



private void checkPermission() { 
    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 


     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 
      // Show an explanation to the user asynchronously -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      return; 
     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
      // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
      return; 
     } 
    } 
} 
0

Wenn Sie einen Dienst, indem Sie den Konstruktor auf es nicht nennen, noch Sie sie außer Kraft setzen. Stattdessen MiServicio servicio = new MiServicio(Context c) in Ihrer Tätigkeit zu verwenden oder wo auch immer Sie versuchen, diesen Dienst zu starten, verwenden Sie den folgenden Befehl ein:

context.startService(new Intent(context, MiServicio.class)); 

Contexts von Android implementiert werden müssen, und in Ihrem Manifest erklärt. Eine vollständige Anleitung zur Verwendung von Android Services finden Sie in Android Services tutorial.

Verwandte Themen