java定时器,cpu唤醒在android 10上不工作

rkkpypqq  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(169)

我在为lovisionandroid用户开发一个会说话的时钟,而我在android10上遇到了问题。我开发了一个服务,它使用timer类检查当前时间,并使用tts类说出小时数。我在manifest文件中设置了wake\ u lock权限,在android7.0和7.1上这可以正常工作,但在android10中不行。这是servicee类的代码,但我将更改一些变量名,因为它们是用塞尔维亚语编写的。

package net.ddns.daremc.dmtalkingclock;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.PowerManager;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.Nullable;

public class DMTCClockService extends Service {
    private Timer timer;
    private talkingClockFunctions talkingclockfunctions;

    @Override
    public void onCreate() {
        super.onCreate();
        talkingclockfunctions = new talkingClockFunctions(this, getString(R.string.format_time), getString(R.string.format_date));
        timer = new Timer();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.timer.cancel();
        this.timer = null;
        try {
            talkingclockfunctions.finalize();
        }
        catch (Throwable gre) {
            gre.printStackTrace();
        }
        this.talkingclockfunctions = null;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    processTimer();
        return START_STICKY;
    }
    private void processTimer() {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                int nMin = Calendar.getInstance().getTime().getMinutes();
                int nSec = Calendar.getInstance().getTime().getSeconds();
                if (nMin < 1 && nSec < 1) {
                    talkingclockfunctions.sayTime();
                }
                wakeupDevice();
            }
        }, 0, 1000);
    }
    private void wakeupDevice() {
        new AsyncTask<Void, Void, Exception>() {
            @Override
            protected Exception doInBackground(Void... voids) {
            try {
                PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
                PowerManager.WakeLock wlc = powerManager.newWakeLock((PowerManager.ON_AFTER_RELEASE | PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "DM Talking Clock - PARTIAL WAKE LOCK");
                wlc.acquire();
                try {
                    Thread.sleep(10000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                wlc.release();
            }
            catch (Exception e) {
                return e;
            }
                return null;
            }
        }.execute();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题