Android 7.0前台服务无法克服打ze模式?

时间:2018-12-28 12:34:35

标签: android android-intent android-service android-7.0-nougat android-doze

嗨,我正在运行Android 7.0的LG(K8)上进行测试,我进行了很多搜索以寻求解决方案,但找不到任何解决方案。问题是当我关闭屏幕时,该服务可能会被暂停(但不会终止),因为TTS和文件写入均不会发生。当屏幕打开或重新打开时,没有问题,TTS和文件写入按预期进行。我怀疑在屏幕关闭且手机无法自行唤醒时无法克服打ze睡模式,尽管据称前台服务可以克服打ze睡模式。我还忽略了(手动)应用程序的电池优化,并且还添加到了清单中,提前知道它对于Android 6或7不会有太大影响。

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

问题仍然存在。任何想法如何解决?特定于此手机吗?以下服务代码:

public class HelloService extends Service  {
    private static final String LOG_TAG = "ForegroundService";
    TextToSpeech tts;
    String strDateFormat = "HH:mm";
    SimpleDateFormat dateFormat = new SimpleDateFormat(strDateFormat);
    PendingIntent pendingIntent;

    public void out(Date dat) throws IOException {
        PrintWriter str = new PrintWriter(new FileWriter("/sdcard/out.txt", true));
        try {
            str.println(dat);
        } finally {
            if (str != null) {
                str.close();
            }
        }
    }


    @Override
    public void onCreate() {
    tts=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status == TextToSpeech.SUCCESS){
            int result=tts.setLanguage(new Locale("en_GB"));
            if(result==TextToSpeech.LANG_MISSING_DATA ||
               result==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e("error", "This Language is not supported");
            }
            }
        }
        }); 
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(LOG_TAG, "Received Start Foreground Intent ");
    Intent notificationIntent = new Intent(this, HelloActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    pendingIntent = PendingIntent.getActivity(this, 0,
                          notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("ServiceTitle")
        .setTicker("ServiceTicker")
        .setContentText("ServiceText")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setOngoing(true).build();

    startForeground(1337,notification);
    Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();

    new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
            try {
                Date date = new Date();
                String text = dateFormat.format(date); 
                if(text==null||"".equals(text))
                {
                    text = "Content not available";
                    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                }
                else {
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                out(date);
                }
            // tts and write every 1 minute
                Thread.sleep(1 * 60000);    
            }
            catch (Exception e) {
            }

            }
        }
        }).start();

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.i(LOG_TAG, "In onDestroy"); 
    if (tts != null) {
        tts.stop();
        tts.shutdown();
        Toast.makeText(this, "Stopped", Toast.LENGTH_SHORT).show();
    }
    super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Used only in case of bound services.
        return null;
    }
}

0 个答案:

没有答案