Android Studio:第一次运行应用程序时,onLocationChanged 在 ForegroundService 中不起作用

问题描述 投票:0回答:0

当我第一次在真实设备上运行该应用程序时,它显示了 ForegroundService 通知,但在 onLocationChanged 中什么也不做。但是,当再次运行该应用程序而不从设备中删除该应用程序时,它运行良好。这使得我在 Play 商店中的应用程序不适用于首次安装它的人,但适用于更新它的人。我该如何解决这个问题?

public class ForegroundService extends Service implements LocationListener {
    boolean isPermissionGranted;
    LocationManager locationManager;

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

        createNotificationChannel();

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        Notification notification = new Notification.Builder(this, "CHANNEL_ID")
                .setSmallIcon(R.drawable.samoyed_)
                .setContentTitle("Woof!")
                .setContentText("Hello!")
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        checkPermission();

        if (isPermissionGranted) {
            LocationUpdate();
        }

        return super.onStartCommand(intent, flags, startId);
    }

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

    private void checkPermission() {
        Dexter.withContext(this).withPermission(Manifest.permission.ACCESS_FINE_LOCATION).withListener(new PermissionListener() {
            @Override
            public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                isPermissionGranted = true;
                LocationUpdate();
            }
            @Override
            public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {}
            @Override
            public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {}
        }).check();
    }

    @SuppressLint("MissingPermission")
    private void LocationUpdate() {
        locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, (float) 0, (LocationListener) this);
    }

    @SuppressLint("MissingPermission")
    public void onLocationChanged(@NonNull Location location) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        Notification notification_ = new Notification.Builder(this, "CHANNEL_ID")
                .setSmallIcon(R.drawable.samoyed_)
                .setContentTitle("Woof!")
                .setContentText("Arrived!")
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification_);
    }

    private void createNotificationChannel() {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_desc);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        stopService(serviceIntent);
        super.onTaskRemoved(rootIntent);
    }
}
java android-studio android-notifications foreground-service on-location-changed
© www.soinside.com 2019 - 2024. All rights reserved.