尝试在Android设备上检索位置...在模拟器上有效,但在实际设备上不起作用

时间:2018-12-04 22:25:15

标签: java android android-studio location location-services

我的问题:我正在尝试创建一个可在大学范围内使用的应用程序。它需要不断访问位置。我在线上跟随了一个教程,并完成了下面的代码。此代码非常适合我的android模拟器,但不适用于我的设备。我在设备上收到许可请求,但没有任何更新。现在,奇怪的是,如果我在设备上使用模拟位置应用程序,则该应用程序可以正常工作。但仅适用于模拟位置应用。

有人对如何解决此问题有任何建议吗? (xml文件只是一个单独的textview。就像我说的那样,我只是在先进行测试)

package com.example.dylan.locationexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private LocationManager locationManager;
    private LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("Location: ", location.toString());

                TextView tv = findViewById(R.id.textView);
                tv.setText(tv.getText() + Double.toString(location.getLatitude()) + "\t" + Double.toString(location.getLongitude()) + "\n");

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };



        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // 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 ActivityCompat#requestPermissions for more details.

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

        }else{

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


            }
        }
    }


}

2 个答案:

答案 0 :(得分:0)

请确保清单中包含以下内容

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

并在您的物理设备上启用gps。

如果仍然遇到问题,请检查手机上该应用程序的权限设置。您可能已拒绝访问位置的权限。

这是我的代码正在使用:

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    com.google.android.gms.location.LocationListener mLocationListener = new com.google.android.gms.location.LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            listener.onLocation(location);
        }
    };
    if(location != null) {
        listener.onLocation(location);
    }else{
        locationListener = new android.location.LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                listener.onLocation(location);
                locationManager.removeUpdates(locationListener);
                Log.d(TAG,"Location: " + String.valueOf(location.getLongitude()));
            }
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }
            @Override
            public void onProviderEnabled(String s) {

            }
            @Override
            public void onProviderDisabled(String s) {

            }
        };
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

答案 1 :(得分:0)

我实际上遇到了同样的事情,我找到了使它在物理设备上运行的解决方案。 禁用WI-FI连接,它会起作用。知道它如何或为什么起作用。现在,我刚刚打开了WI-Fi连接,它又可以正常工作了。如果您无法使用Wi-Fi,请尝试重启设备

在房屋中移动时,您可能还需要稍等片刻,然后才能开始获取位置。