Решительно подождите на экране блокировки несколько раз, таймер зависает - PullRequest
0 голосов
/ 19 июня 2020

Мой сервис работает. При закрытии приложения и блокировке устройства службы продолжают работать, но через несколько минут уведомление зависает. Например таймер обратного отсчета запускается через 10 минут. И близкое приложение. И запереть устройство. через 30 минут разблокируйте устройство и посмотрите уведомление. Таймер остановился примерно на 3:15. Обычно звук уведомления воспроизводится через 10 минут. Из-за этой ситуации звук уведомления воспроизводится 30 минут (так что разблокировка устройства минута> 10 минут)

ExampleService

 import static com.e.pomodoro.Bildirim.App.CHANNEL_1_ID;
    import static com.e.pomodoro.Bildirim.App.CHANNEL_2_ID;



public class ExampleService extends Service {
    private CountDownTimer mCountDownTimer = null;
    private long mTimeLeftInMillis;
    private String timeLeftFormatted;
    boolean calisiyormu;
    SharedPreferences arkaplanPref;
    SharedPreferences.Editor editor;
    NotificationManagerCompat notificationManager;
    private boolean molaCcalisiyormu;
    public static final String DURDUR = "com.e.pomodoro.durdur";
    long l;
    public PowerManager.WakeLock wakeLock;
    private PowerManager.WakeLock wl;

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

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    cancelTimer();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        arkaplanPref = this.getSharedPreferences("arkaplanPref", Context.MODE_PRIVATE);
        editor = arkaplanPref.edit();
        String surec = arkaplanPref.getString("surec" ,"pomodoro");
        mTimeLeftInMillis = arkaplanPref.getLong("calismaSuresi",0);
        l= mTimeLeftInMillis / 5;
        switch (surec){

            case "pomodoro":
                startTimer();
                break;
            case "kisamola":
                startMola();
                break;
            case "uzunmola":

                break;

        }

        return START_NOT_STICKY;
    }

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


    private void startTimer() {



        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {


                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();


                String ss = getResources().getString(R.string.worktime);
                if (mTimeLeftInMillis>l*4){
                    Bildirim(timeLeftFormatted,ss,false,R.drawable.bilgirim1);
                }
                if (mTimeLeftInMillis>l*3 && mTimeLeftInMillis<l*4 ){
                    Bildirim(timeLeftFormatted,ss,false,R.drawable.bilgirim2);
                }
                if (mTimeLeftInMillis>l*2 && mTimeLeftInMillis<l*3){
                    Bildirim(timeLeftFormatted,ss,false,R.drawable.bilgirim3);
                }
                if (mTimeLeftInMillis>l*1 && mTimeLeftInMillis<l*2){
                    Bildirim(timeLeftFormatted,ss,false,R.drawable.bilgirim4);
                }
                if (mTimeLeftInMillis>0 && mTimeLeftInMillis<l*1){
                    Bildirim(timeLeftFormatted,ss,false,R.drawable.bilgirim5);
                }





              //  Bildirim(timeLeftFormatted,false,agaclar[0]);
                calisiyormu = true;
                editor.putBoolean("molaCcalisiyormu",false); //boolean değer ekleniyor
                editor.putBoolean("calisiyormu",calisiyormu); //boolean değer ekleniyor
                editor.putString("zaman",timeLeftFormatted); //boolean değer ekleniyor
                editor.putLong("zmn", mTimeLeftInMillis);
                editor.commit(); //Kayıt
                editor.apply();
            }

            @Override
            public void onFinish() {
                calisiyormu = false;
                editor.putBoolean("molaCcalisiyormu",false); //boolean değer ekleniyor
                editor.putBoolean("calisiyormu",calisiyormu); //boolean değer ekleniyor
                editor.putBoolean("calismaSuresiBitti",calisiyormu);
                editor.putBoolean("molaZamaniMi",true); //boolean değer ekleniyor
                editor.commit(); //Kayıt
                editor.apply();
                stopForeground(true);

                SendOnChannel1(getResources().getString(R.string.tekrikler),getResources().getString(R.string.pomodorotamamlandi));
             // Bildirim("Pomodoro Tamamlandı",true);
                // updateCountDownText();

                cancelTimer();

                cancel();
            }

        }.start();

    }

    private void startMola() {



        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {


                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                String ss = getResources().getString(R.string.breaktime);
                Bildirim(timeLeftFormatted,ss,false,R.drawable.bell);
                molaCcalisiyormu = true;
                editor.putBoolean("molaCcalisiyormu",molaCcalisiyormu); //boolean değer ekleniyor
                editor.putBoolean("calisiyormu",false); //boolean değer ekleniyor

                editor.putString("zaman",timeLeftFormatted); //boolean değer ekleniyor
                editor.putLong("zmn", mTimeLeftInMillis);
                editor.commit(); //Kayıt
                editor.apply();


            }

            @Override
            public void onFinish() {
                molaCcalisiyormu = false;
                editor.putBoolean("molaCcalisiyormu",molaCcalisiyormu); //boolean değer ekleniyor
                editor.putBoolean("calisiyormu",false); //boolean değer ekleniyor
                editor.putBoolean("calismaSuresiBitti",calisiyormu);
                editor.putBoolean("molaZamaniMi",false); //boolean değer ekleniyor
                editor.commit(); //Kayıt
                editor.apply();

                stopForeground(true);

                SendOnChannel1(getResources().getString(R.string.molabitti),getResources().getString(R.string.calismayadevam));

                // updateCountDownText();
                // stopSelf();
                cancelTimer();
                cancel();
            }

        }.start();

    }





    public void cancelTimer() {
        calisiyormu = false;
        editor.putBoolean("calisiyormu",calisiyormu); //boolean değer ekleniyor
        editor.commit(); //Kayıt
        editor.apply();

        if(mCountDownTimer!=null)
            mCountDownTimer.cancel();
        mTimeLeftInMillis = 100000;
        // updateCountDownText();

    }
    private void updateCountDownText() {
        int minutes = (int) (mTimeLeftInMillis/ 1000) / 60;
        int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
        timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);

    }


    public void Bildirim(String s, String ss,boolean iptal,int i){

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

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),i);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(ExampleService.this,CHANNEL_2_ID)
                .setContentTitle(ss)
                .setContentText(s)
                .setLargeIcon(bitmap)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentIntent(pendingIntent)
                .setOnlyAlertOnce(true)
                .setOngoing(false)
                .setAutoCancel(iptal);

      startForeground(1,notification.build());

    }



    public void SendOnChannel1(String baslik, String icerik){

        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );

        PendingIntent resultPendingIntent = PendingIntent.getActivity(ExampleService.this,0,resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bilgirim6);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle(baslik)
                .setContentText(icerik)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_REMINDER)
                .setContentIntent(resultPendingIntent)
                .setAutoCancel(true)
              //  .addAction(R.drawable.pomodoro,"kapat",actionIntent)
                .setOnlyAlertOnce(true)
                .setLargeIcon(bitmap)

                .setOngoing(false)
                .build();

      //  startForeground(101,notification);
        notificationManager = NotificationManagerCompat.from(this);
       notificationManager.notify(1,notification);



    }



}
...