Android Сервис никогда не останавливается, даже если приложение удалено из системного трея - PullRequest
0 голосов
/ 24 апреля 2020

Доброе утро, я разрабатываю приложение, которое управляет любимыми предметами. Я разработал Сервис, который каждые 30 секунд ищет обновления с удаленной БД и, в конечном итоге, обновляет локальную БД и отправляет уведомления. Если приложение работает, оно работает отлично, но если я удаляю приложение из системного трея, я не могу получать уведомления: они будут приходить только после повторного открытия приложения.

Это параметры:

minSdkVersion 16 targetSdkVersion 29

Мой сервис:

public class UpdateFavoritesService extends Service {

    public Runnable mRunnable = null;
    String strDate,strDateText;
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat dateFormatText = new SimpleDateFormat("EEE, dd MMMM yyyy");
    Context context;

    public UpdateFavoritesService(Context applicationContext) {
        super();
        context = applicationContext;
        Log.i("HERE", "here service created!");
    }

    public UpdateFavoritesService() {

    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Handler mHandler = new Handler();
        strDate = dateFormat.format(date);
        strDateText = dateFormatText.format(date);
        mRunnable = new Runnable() {
            @Override
            public void run() {
                RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
                DatabaseHelper myDb = new DatabaseHelper(getApplicationContext());
                Cursor resultCursor = myDb.getAllRowsPlaying();
                while (resultCursor.moveToNext()) {
                    Date date= new Date();

                    long time = date.getTime();
                    System.out.println("Time in Milliseconds: " + time);

                    Timestamp ts = new Timestamp(time);
                    int id = resultCursor.getInt(resultCursor.getColumnIndex("ID"));
                    String id_lega=resultCursor.getString(resultCursor.getColumnIndex("ID_LEGA"));
                    String name_lega=resultCursor.getString(resultCursor.getColumnIndex("NAME_LEGA"));
                    String flag_lega=resultCursor.getString(resultCursor.getColumnIndex("FLAG_LEGA"));
                    Log.v("ID GARA: ", id + " "+ts);
                    updateMatch(myDb,id,queue,id_lega,name_lega,flag_lega);

                }
                mHandler.postDelayed(mRunnable, 30 * 1000);
            }
        };
        mHandler.postDelayed(mRunnable, 15 * 1000);

        return START_STICKY;
    }

Я звоню по активности:

UpdateFavoritesService mSensorService = new UpdateFavoritesService(this);
Intent mServiceIntent = new Intent(getApplicationContext(), mSensorService.getClass());
startService(mServiceIntent);

и это мой манифест:

<service
 android:name=".service.UpdateFavoritesService"
 android:enabled="true" />

Как я могу решить? Спасибо за вашу помощь

...