最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Why does android LocationRequest run once instead of continuously in android studio? - Stack Overflow

programmeradmin3浏览0评论

I have implemented this request which previously worked and returned multiple locations but now it gets one then stops it seems. I can see with logs that I only receive 1 location update. I have checked permissions as well as rebuilding and that sort of thing. Not sure where to go from here. I have tried switching accuracy from high to balanced but that made no difference either. I have gone back to previous versions where I know this worked and still no luck suggesting a problem with the emulator or android studio. I have enabled precise location when using the app as well as taken away battery restrictions on the emulator and still no luck. Any suggestions would be appreciated! going to try an actual phone soon to see if that makes any difference. The code is running in a method which is called upon user clicking a button in the fragment. the location updates are stopped when that button is clicked again. Strange that its suddenly stopped working properly and is getting one location response and then stopping.

I ran the app on my physical device and it worked fine.

    LocationRequest locationRequest = new 
    LocationRequest.Builder(Priority.PRIORITY_BALANCED_POWER_ACCURACY, 300).build();


    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(@NonNull LocationResult locationResult) {
            Log.d("LocationUpdates", "Location count: " + 
            locationResult.getLocations().size());

            for (Location location : locationResult.getLocations()) {
                if (location != null) {
                    float speed = location.getSpeed(); // Speed in meters/second
                    float speedKmh = speed * 3.6f; // Convert to km/h
                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();


                    // Save location to list
                    locationList.add(new LocationData(latitude, longitude, speedKmh));


                    // Log & Display Location Data
                    Log.d("Location", "Lat: " + latitude + ", Lng: " + longitude);
                    Log.d("Speed", "Current Speed: " + speedKmh + " km/h");


                    // Update UI with current speed
                    txt_accY.setText("Speed: " + speedKmh + " km/h");
                    txt_accZ.setText("Lat: " + latitude + ", Lng: " + longitude);
                }
            }
        }
    };`
发布评论

评论列表(0)

  1. 暂无评论