Как остановить службу в 8 вечера, если она работает? - PullRequest
0 голосов
/ 17 марта 2020

У меня есть служба, которая фиксирует местоположение пользователя и обновляет базу данных, используя модернизацию. Я хочу автоматически останавливать службу в 8 часов вечера каждый день, если она работает, а также обновлять базу данных, которую пользователь выбил в 8 часов вечера.

Я хочу, чтобы служба запускалась вручную, но хочу, чтобы служба автоматически отключалась если он не был остановлен вручную.

Вот мой класс обслуживания

public class LiveLocationService extends Service {
    private static final String TAG = LiveLocationService.class.getSimpleName();
    Retrofit retrofitClient;
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    MyService myService;
    String empCode, year, month, date;
    FusedLocationProviderClient client;
    LocationCallback locationCallback;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        retrofitClient = RetrofitClient.getInstance();
        myService = retrofitClient.create(MyService.class);

        empCode = intent.getStringExtra("empCode");
        year = intent.getStringExtra("year");
        month = intent.getStringExtra("month");
        date = intent.getStringExtra("date");
        return super.onStartCommand(intent, flags, startId);
    }

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

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate() {
        super.onCreate();
        if (isOnline()) {
            buildNotification();
            requestLocationUpdates();
        } else {
            Log.e("MSER", "Please connect to the internet.");
        }
    }

    private void buildNotification() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String NOTIFICATION_CHANNEL_ID = "com.deepankmehta.managementservices";
            String channelName = "My Background Service";
            NotificationChannel chan = null;
            chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
            chan.setLightColor(Color.BLUE);
            chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(chan);

            PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            Notification notification = notificationBuilder.setOngoing(true)
                    .setSmallIcon(R.drawable.mser)
                    .setContentTitle("xxxx")
                    .setContentText("xxxx is tracking your location.")
                    .setPriority(NotificationManager.IMPORTANCE_HIGH)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .setContentIntent(intent)
                    .build();
            startForeground(2, notification);
        } else {
            PendingIntent broadcastIntent = PendingIntent.getActivity(
                    this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
            // Create the persistent notification
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("xxxx is tracking your location.")
                    .setOngoing(true)
                    .setContentIntent(broadcastIntent)
                    .setSmallIcon(R.drawable.mser);
            startForeground(1, builder.build());
        }

    }

    private void requestLocationUpdates() {
        if (isOnline()) {
            LocationRequest request = new LocationRequest();
            request.setInterval(10000);
            request.setFastestInterval(5000);
            request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            client = LocationServices.getFusedLocationProviderClient(this);
            int permission = ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION);
            if (permission == PackageManager.PERMISSION_GRANTED) {
                locationCallback = new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        Location location = locationResult.getLastLocation();
                        if (location != null) {
                            Log.d(TAG, "location update " + location);
                            double lat = location.getLatitude();
                            double lon = location.getLongitude();
                            final String time = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date());
                            compositeDisposable.add(myService.userLocation(empCode, year, month, date, time, lat, lon)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer< String >() {
                                        @Override
                                        public void accept(String s) throws Exception {
                                            Log.e("data", s);
                                            if (s.equals("\"done\"")) {
                                                Log.e("status", "location punched");
                                            }
                                        }
                                    }));
                        } else {
                            Log.d("MSER", "location update, no location found. ");
                        }
                    }
                };
                client.requestLocationUpdates(request, locationCallback, null);
            } else {
                Log.e("MSER", "Please enable location.");
            }
        } else {
            Log.e("MSER", "Please connect to the internet.");
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        client.removeLocationUpdates(locationCallback);
        stopForeground(true);
        stopSelf();
    }

    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }
}

Вот класс домашней активности: запуск и остановка методов обслуживания. Эти методы вызываются, когда пользователь нажимает кнопки punch in и punch out соответственно.

 private void startTrackerService() {
        final String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
        final String year = date.substring(0, 4);
        final String month = date.substring(5, 7);
        final String dateToday = date.substring(8, 10);

        Intent intent = new Intent(this, LiveLocationService.class);
        intent.putExtra("empCode", empCode);
        intent.putExtra("year", year);
        intent.putExtra("month", month);
        intent.putExtra("date", dateToday);
        startService(intent);
    }

    private void stopTrackerService() {
        stopService(new Intent(this, LiveLocationService.class));
    }

1 Ответ

0 голосов
/ 17 марта 2020

Вы можете использовать AlarmManager , чтобы запланировать задачу такого рода, зарегистрировать AlaramManger в указанное время c и проверить, работает ли служба, если она работает, затем остановите службу.

Вот пример регистрации AlaramManager в указанное время c время

AlarmManager alarmManager = (AlarmManager) getActivityContext()
                .getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(getActivityContext(), AutoStopReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivityContext(),
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                stopServieTime, pendingIntent);

Вот класс получателя,

public class AutoStopReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
          //TODO Stop service from here
    }

Регистрация получателя в AndroidMenifest. xml

<receiver android:name=".AutoStopReceiver" />
...