2016-04-09 97 views
0

我使用下面的代碼來獲取用戶爲了獲得當前位置

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

} 

}

但這裏的onLocationChanged功能永遠不會得到所謂的位置,以便即時通訊無法獲取位置。那麼我如何獲得位置? 在logcat中,該信息選項卡顯示的標題下的緯度和經度onLocationReceived」。

這是明顯的,

<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

你在你的AndroidManifest.xml文件中有什麼? – Eenvincible

+0

@Eenvincible我已經添加了我的清單文件。 – Anurag

回答

0

Google Play services location APIs優於Android框架位置API(android.location )作爲

添加位置感知到你的應用程序的方式。你可以找到的例子如何使用它的權利here

PS不要忘了檢查你的設備授予的權限,如果你添加權限到清單文件...

+0

我使用了Google Play服務,但是在logcat中「GoogleService未能初始化,狀態:10,缺少預期的資源:'R.string.google_app_id'用於初始化Google服務。可能的原因是缺少google-services.json或com.google.gms.google服務gradle插件「。這是即將到來的。 – Anurag

+0

你的Gradle文件怎麼樣? – Eenvincible