Java 位置侦听器未运行my onLocationChanged

Java 位置侦听器未运行my onLocationChanged,java,android,android-location,locationlistener,Java,Android,Android Location,Locationlistener,onLocationChanged中的My buildLocationEvent(位置)从未运行?为什么会这样呢?我看不出任何原因,也许我遗漏了一些明显的东西,请告诉我这里出了什么问题,以及我如何对其进行排序,以便我可以开始正确地获取位置数据 我复制了以下代码: public class LService extends Service { public static final String TAG = "Lervice"; private LocationListener

onLocationChanged中的My buildLocationEvent(位置)从未运行?为什么会这样呢?我看不出任何原因,也许我遗漏了一些明显的东西,请告诉我这里出了什么问题,以及我如何对其进行排序,以便我可以开始正确地获取位置数据

我复制了以下代码:

public class LService extends Service {

    public static final String TAG = "Lervice";
    private LocationListener listener;
    private LocationManager manager;
    private static LocationEvent locationEvent = new LocationEvent();


    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("MissingPermission")
    @Override
    public void onCreate() {
        super.onCreate();
        startMyOwnForeground();

        listener = new LocationListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onLocationChanged(Location location) {
                buildLocationEvent(location);
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        };

        manager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, listener);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        return START_STICKY;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground() {
        String NOTIFICATION_CHANNEL_ID = "com.app.myapp";
        String channelName = "LService";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.GREEN);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(3, notification);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(manager != null){
            manager.removeUpdates(listener);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void buildLocationEvent(Location location){
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        double[] coordinate = {latLng.latitude, latLng.longitude};
        locationEvent.setCoordinate(coordinate);
        locationEvent.setAltitude(location.getAltitude());
        locationEvent.setHorizontalAccuracy(location.getAccuracy());
        locationEvent.setVerticalAccuracy(location.getVerticalAccuracyMeters());
        locationEvent.setTimestamp(location.getTime());
        sendLocationToManager(locationEvent);
    }

    private void sendLocationToManager(LocationEvent locationEvent){
        Intent intent = new Intent("locationCreated");
        sendLocalBroadcastEvent(intent, locationEvent);
    }

    private void sendLocalBroadcastEvent(Intent intent, LocationEvent locationEvent){
        intent.putExtra("locationevent", locationEvent);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}
谢谢你在这方面的帮助。如果你需要澄清什么,请告诉我

当我运行以下命令时,将在Main中调用权限:

String[] appPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION
    };
    private static final int PERMISSIONS_REQUEST_CODE = 1240;


        //get permissions for location
        Log.d(TAG, "permissions: "+ checkAndRequestPermissions()); //false

        if(true) {

            //I RUN MY CODE HERE
        }

    }


    private boolean checkAndRequestPermissions() {
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String perm : appPermissions) {
            if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(perm);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                    PERMISSIONS_REQUEST_CODE
            );
            return false;
        }
        //All Permissions granted
        return true;
    }

可能是权限问题-哪个API版本-后台服务在API 29 ACCESS\u background中有新的权限类型_LOCATION@Andy谢谢,当我第一次运行应用程序时,它确实会请求我一直授予的位置权限。然后再次运行它不会再次询问,并在通知允许后台运行。我刚刚测试过,位置会在模拟器上返回,但不会在我的手机上!所以你可能是对的,我有什么办法可以解决这个问题吗?主要是在问题中添加了权限请求。我正在调用传感器数据和位置数据。如果没有在单独的线程上调用它们,这会是一个问题吗?您不是第一次或每次都获取位置数据吗?可能是权限问题-哪个API版本-后台服务中有新的权限类型API 29,访问背景_LOCATION@Andy谢谢,当我第一次运行应用程序时,它确实会请求我一直授予的位置权限。然后再次运行它不会再次询问,并在通知允许后台运行。我刚刚测试过,位置会在模拟器上返回,但不会在我的手机上!所以你可能是对的,我有什么方法可以解决这个问题吗?主要是在问题中添加了权限请求。我正在调用传感器数据和位置数据。如果它们不是在单独的线程上调用,这会是一个问题吗?你不是只第一次还是每次都获取位置数据?
    public void startUpdatingLocation(){
        //Start sensor service
        Intent locationIntent = new Intent(MyApplication.getAppContext(), LService.class);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            MyApplication.getAppContext().startForegroundService(locationIntent);
        } else {
            MyApplication.getAppContext().startService(locationIntent);
        }

    }