Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取当前位置_Android_Locationlistener - Fatal编程技术网

Android 获取当前位置

Android 获取当前位置,android,locationlistener,Android,Locationlistener,我正在使用下面的代码获取用户的位置 public class MainLocation extends Application implements LocationListener { protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; String lat; String provider; protected St

我正在使用下面的代码获取用户的位置

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>

与Android框架位置API(Android.location)相比,它们更适合作为向应用程序添加位置感知的一种方式

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


注意:如果您将权限添加到清单文件中,请不要忘记检查您的设备上是否授予了权限…

您的AndroidManifest.xml文件中有什么?@Eenvincible我已添加了清单文件。我使用了Google Play服务,但在logcat中“Google服务未能初始化,状态:10,缺少用于初始化google服务的预期资源:“R.string.google\u app\u id”。可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。“这就来了。你的gradle文件呢?”?