2016-04-09 74 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