Как запустить сервис переднего плана на 10 секунд, каждые 5 минут? - PullRequest
1 голос
/ 11 ноября 2019

Я хочу запустить Foreground Service независимо от того, закрыто приложение или открыто, скажем, 10 секунд. По истечении этих 10 секунд Foreground Service следует уничтожить и через 5 минут вызвать снова. Foreground Service, который я нашел, находит местоположение пользователя и затем сохраняет его в базе данных SQLite. Поэтому в основном я хочу сохранять новые координаты один раз, каждые 5 минут, чтобы я мог улучшить расход заряда батареи.

Мой класс обслуживания:

public class LocationService extends Service{

private final static int UPDATE_TIME = 10000;
private final static int UPDATE_DISTANCE = 0;

private LocationListener locationListener;
private LocationManager locationManager;

private PowerManager.WakeLock mWakeLock;

private String latitude, longitude;

private DatabaseHelper myDb;

@Override
public void onCreate() {
    super.onCreate();

    myDb = new DatabaseHelper(this);
    locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    createWakeLock(); //Creates a wakelock
    createNotification(); //Creates a notification if SDK version is > 26

}

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

    mWakeLock.acquire(10*60*500L /*5 minutes*/);

    locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

            Log.d("Location", "Updated");

            getCoordinates(location); //Gets coordinates
            insertCoordinates(latitude, longitude); //Sends coordinates to SQLite database
            sendDataToMainActivity(latitude, longitude); //Sends coordinates to MainActivity

        }

        @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);

        }

    };

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, UPDATE_TIME, UPDATE_DISTANCE, locationListener);

    return START_NOT_STICKY;

}

@Override
public void onDestroy() {

    super.onDestroy();

    Log.d("Service", "Destroyed");

    if(locationManager != null)
        locationManager.removeUpdates(locationListener);

    mWakeLock.release();

}

private void createWakeLock() {

    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);

    if(pm != null)
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "myApp:myWakeLock");

}

private void createNotification() {

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

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Service")
            .setContentText("Coordinates Location Running")
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1, notification);

}

private void getCoordinates(Location location) {

    this.latitude = String.valueOf(location.getLatitude());
    this.longitude = String.valueOf(location.getLongitude());

}

private void insertCoordinates(String latitude, String longitude) {

    boolean inserted = myDb.insertData(latitude, longitude); //Insert coordinates

    //Check if insertion is completed
    if(inserted)
        Toast.makeText(LocationService.this, "Coordinates Inserted", Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(LocationService.this, "Coordinates Not Inserted", Toast.LENGTH_SHORT).show();

}

private void sendDataToMainActivity(String latitude, String longitude) {

    Intent i = new Intent("location_update");
    i.putExtra("latitude", latitude);
    i.putExtra("longitude",longitude);

    sendBroadcast(i);

}

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

Ответы [ 3 ]

1 голос
/ 11 ноября 2019

Используйте Fused Location Api для запроса обновлений местоположения, что вызовет ожидающее намерение (скажем, запускает службу намерений) в указанный интервал повторения (дать 5 минут). Поэтому Fused Location Api читает местоположение каждые 5 минут и отправляет егов службу намерений, откуда вы можете провести обработку.

Пожалуйста, обратитесь это для более подробной информации.

0 голосов
/ 12 ноября 2019

«Fused Location Api» может не работать в фоновом режиме из-за ограничений управления питанием. Вы должны использовать Alarm Manager, но есть некоторые ограничения в последних версиях Android. Подробнее о DOZE здесь

    AlarmManager alarm =
            (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    if (alarm == null) return;  // for instant apps

    Intent intent = new Intent(this, MyBroadcastReceiver.class)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerInMillis,
                PendingIntent.getBroadcast(this, intent, PendingIntent.FLAG_UPDATE_CURRENT));

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AlarmManager.AlarmClockInfo ac=
                new AlarmManager.AlarmClockInfo(triggerInMillis, null);
        alarm.setAlarmClock(ac, PendingIntent.getBroadcast(this, intent, PendingIntent.FLAG_UPDATE_CURRENT));

    } else {
        alarm.setExact(AlarmManager.RTC_WAKEUP, triggerInMillis,
                PendingIntent.getBroadcast(this, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
0 голосов
/ 11 ноября 2019

Вы можете использовать Alarm Manager, чтобы установить точную тревогу, запустить службу в функции тревоги при получении, запустить службу переднего плана на 5 минут, чем установить следующую тревогу

...