Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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 requestLocationUpdates()花费太多时间,或者根本不起作用_Android_Android Gps - Fatal编程技术网

Android requestLocationUpdates()花费太多时间,或者根本不起作用

Android requestLocationUpdates()花费太多时间,或者根本不起作用,android,android-gps,Android,Android Gps,我在Android应用程序中使用了requestLocationUpdates方法,每当我的设备位置发生变化时,我都会在Service中获取更新。我已经设置了minTime=0,但是这个requestLocationUpdates方法会在很长的延迟后(即10到15分钟,甚至更长)进行更新。有时,当我运行应用程序时,requestLocationUpdatesmehod根本不提供任何更新。而且这种requestLocationUpdates方法很少(非常罕见)像预期的那样运行(以0秒的延迟连续进行

我在Android应用程序中使用了
requestLocationUpdates
方法,每当我的设备位置发生变化时,我都会在
Service
中获取更新。我已经设置了
minTime=0
,但是这个
requestLocationUpdates
方法会在很长的延迟后(即10到15分钟,甚至更长)进行更新。有时,当我运行应用程序时,
requestLocationUpdates
mehod根本不提供任何更新。而且这种
requestLocationUpdates
方法很少(非常罕见)像预期的那样运行(以0秒的延迟连续进行更新)。那么问题出在哪里呢?我如何在每次运行应用程序时不断获得更新

HelloService.java

public class HelloService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(MainActivity.context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        else {
                            manager.requestLocationUpdates("gps", 0, 0, new LocationDetector());
                            Toast.makeText(MainActivity.ctx, "Service Started...", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();
        return Service.START_STICKY;
    }
}
public class LocationDetector implements LocationListener {    
    @Override
    public void onLocationChanged(Location location) {
        /*I have used here Log.e instead of Log.d in order to get the output in clear red message that is easily readable*/
        Log.e("GPS: ", location.getLatitude()+ ", " +location.getLongitude(), new Exception());
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onProviderDisabled(String provider) {}
}
LocationDetector.java

public class HelloService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(MainActivity.context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        else {
                            manager.requestLocationUpdates("gps", 0, 0, new LocationDetector());
                            Toast.makeText(MainActivity.ctx, "Service Started...", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();
        return Service.START_STICKY;
    }
}
public class LocationDetector implements LocationListener {    
    @Override
    public void onLocationChanged(Location location) {
        /*I have used here Log.e instead of Log.d in order to get the output in clear red message that is easily readable*/
        Log.e("GPS: ", location.getLatitude()+ ", " +location.getLongitude(), new Exception());
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onProviderDisabled(String provider) {}
}

我尝试了你的全部代码,在我的笔记本电脑上创建新项目,并在我的手机上运行

结果:它的工作和全球定位系统的TTF是几乎15秒在我的手机上

你试过其他设备吗

gps图标呢?出现,消失


然后尝试fused location provider,看看会发生什么,

您请求location manager每0秒和每0米返回一次您的位置。那么你到底期望什么样的行为呢?到GPS实际工作的地方去。这是一个复制品。您现在几乎已经开始出现垃圾邮件堆栈溢出。您是否每秒钟都会收到一次更新,每次运行应用程序时?我都会收到不同时间段的位置更新,因为它在诸如minTime=0和minDistance=0这样的攻击性使用情况下无法正常工作。您在执行过程中观察到的两次更新之间的最大延迟是多少?第一次修复后(15秒),延迟在0-2000毫秒之间。我已经等待了30分钟,但没有收到更新。gps图标正在出现。那么,这是设备gps的问题吗?