手机锁定时计时器停止工作?前台服务中的计时器

问题描述 投票:0回答:1

我想做什么:每隔10分钟显示一次对话框,与应用程序状态(关闭或打开应用程序)和电话状态(电话处于锁定状态,空闲状态或用户正在使用其他应用程序)无关。

我尝试过的:

  1. 前台服务中的10分钟计时器-仅在用户使用电话时才使用,无论应用程序状态如何。如果手机已锁定,则不会显示该对话框。

我正在使用带有START_STICKY标志的前台服务。

服务代码:

public class QuestionnaireService extends Service {

    private static final String TAG = QuestionnaireService.class.getSimpleName();
    private Timer timer;
    private long displayQuestionTimeout;

    @Override
    public void onCreate() {
        super.onCreate();try {
            displayQuestionTimeout = Constants.QUESTIONAIRE_INTERVAL * 60 * 1000; // 10 Minutes
            timer = new Timer();


            if (Build.VERSION.SDK_INT >= 26) {
                String name="Question is running";
                NotificationChannel channel = new NotificationChannel(Constants.QUESTIONAIRE_NOTIFICATION_CHANNEL,
                        name,
                        NotificationManager.IMPORTANCE_DEFAULT);

                ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

                Notification notification = new NotificationCompat.Builder(this, Constants.QUESTIONAIRE_NOTIFICATION_CHANNEL)
                        .setContentTitle("TimeoutIQ Question is Active")
                        .setCategory(NotificationCompat.CATEGORY_SERVICE)
                        .setContentText(name)
                        .setVisibility(Notification.VISIBILITY_PUBLIC)
                        .setOngoing(true)
                        .build();

                startForeground(Constants.QUESTION_REQUEST_CODE, notification);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroy() {
        Log.e(TAG, " ==>> \n\n Question Destroyed \n\n ==>>");
        try {
            if (timer != null) {
                timer.cancel();
            }
            if (EventBus.getDefault().isRegistered(ParentHomeActivity.class))
                EventBus.getDefault().unregister(ParentHomeActivity.class);
            if (EventBus.getDefault().isRegistered(ChildHomeActivity.class))
                EventBus.getDefault().unregister(ChildHomeActivity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
//        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onLowMemory() {
//        super.onLowMemory();
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {

        if (intent!=null){
            if (intent.getAction()!=null && intent.getAction().equals(Constants.ACTION.STOP_ACTION)) {
                Log.i("QUestionaire ", "Stop servive ");
                stopForeground(true);
                stopSelf();
            }
        }



        if (timer != null) {
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                   //doing task here

                }
            }, displayQuestionTimeout, displayQuestionTimeout);
        }
        return START_STICKY;
    }


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


}

我如何致电服务:

 Intent intent=new Intent(context, QuestionnaireService.class);
 ContextCompat.startForegroundService(AppController.getContext(),intent);

清单文件:

<service
       android:name=".child.service.questionnaire.QuestionnaireService"
        android:enabled="true"
          android:exported="false" />

我已经尝试在清单文件中添加process, android:directBootAware=" true",但没有任何效果。

在计时器的run()函数下编写的代码在应用关闭时停止调用。

编辑1:在前台服务的情况下,即使关闭应用程序,但我的通知仍能完美显示,但代码未在运行功能下编写。

android service dialog background-process foreground-service
1个回答
-1
投票

[如果您希望即使用户不进行交互,前景服务也可以连续运行,则该应用程序必须显示一个通知。

仅运行带有不可取消通知的服务,除非您希望服务停止,然后您可以通过Toast显示消息。

© www.soinside.com 2019 - 2024. All rights reserved.