2016-04-09 14 views
0

Ich verwende den folgenden Code zu erhalten, um den Standort eines Benutzers zu erhaltenUm den aktuellen Standort

public class MainLocation extends Application implements LocationListener { 
protected LocationManager locationManager; 
protected LocationListener locationListener; 
protected Context context; 
String lat; 
String provider; 
protected String latitude, longitude; 
protected boolean gps_enabled, network_enabled; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    Log.e("Location", "here"); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
      // 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 Activity#requestPermissions for more details. 
      return; 
     } 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 


} 

@Override 
public void onLocationChanged(Location location) { 
    Log.e("Location", "" + location.getLatitude()); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    Log.e("Location Status", "" + provider + " " + status + " " + extras); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    Log.e("Location", "Enabled"); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Log.e("Location","Disabled"+provider); 

} 

}

aber hier ist die OnLocationChanged Funktion wird nie bekommen, so genannt, im die Lage nicht in der Lage holen . Wie kann ich den Standort bekommen? Im logcat wird die Registerkarte Info Breite und Länge unter dem Titel zeigt onLocationReceived“.

Dies ist das Manifest ist,

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    package="com.example.krijjj.loginapp" > 
    <!-- 
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but are recommended. 
    --> 
    <uses-sdk android:minSdkVersion="15" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    <application 
     android:name=".Location.MainLocation" 
     android:allowBackup="true" 
     android:icon="@drawable/icon1" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     tools:replace="android:label" > 
     <activity 
      android:name=".Track" 
      android:label="Track" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Security.Lockscreen" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Second" 
      android:label="@string/title_activity_second" > 
      <intent-filter> 
       <action android:name="com.example.krijjj.loginapp.Second" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_mapactivity" > 
     </activity> 
     <activity 
      android:name=".Security.MainActivity" 
      android:label="@string/title_activity_mapactivity" > 
     </activity> 
     <!-- 
      The API key for Google Maps-based APIs is defined as a string resource. 
      (See the file "res/values/google_maps_api.xml"). 
      Note that the API key is linked to the encryption key used to sign the APK. 
      You need a different API key for each encryption key, including the release key that is used to 
      sign the APK for publishing. 
      You can define the keys for the debug and release targets in src/debug/ and src/release/. 
     --> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 

     <activity 
      android:name=".MapsActivity" 
      android:label="@string/title_activity_maps" > 
     </activity> 
    </application> 

</manifest> 
+0

Was haben Sie in Ihrer Datei "AndroidManifest.xml"? – Eenvincible

+0

@Eenvincible Ich habe meine Manifest-Datei hinzugefügt. – Anurag

Antwort

0

Die Google Play services location APIs werden über die Android Framework Lage APIs bevorzugt (android.location) als eine Möglichkeit, Standorterkennung, um Ihre Anwendung hinzuzufügen.

können Sie das Beispiel zu finden, wie es zu benutzen, rechts here.

PS vergessen Sie nicht, Überprüfen Sie, ob auf Ihrem Gerät Berechtigungen erteilt wurden, wenn Sie Berechtigungen in die Manifestdatei hinzugefügt haben ...

+0

Ich habe Google Play Services verwendet, aber darin im Logcat "GoogleService konnte nicht initialisiert werden, Status: 10, Es fehlt eine erwartete Ressource: 'R.string.google_app_id" zur Initialisierung von Google-Diensten. Mögliche Ursachen sind google-services.json oder com.google.gms.google-services gradle plugin. " Das steht vor der Tür. – Anurag

+0

Was ist mit Ihrer Gradle-Datei? – Eenvincible

Verwandte Themen