获取当前位置

时间:2016-04-09 12:22:18

标签: android locationlistener

我使用以下代码获取用户的位置

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>

1 个答案:

答案 0 :(得分:0)

Google Play services location APIs优先于Android框架位置API(android.location),作为向您的应用添加位置感知的一种方式。

您可以找到如何正确使用here的示例。

P.S。如果您在清单文件中添加了权限,请不要忘记检查您的设备是否已授予权限...