A service that runs every day in the background on android

0

Good afternoon!    Maybe my question does not fit perfectly into the criteria because it is more of a doubt whether "it is possible or it is not possible".    Here's the problem:    I want to create an app in the following scenario: it will set how long the mobile screen goes off after a certain time, even if the app in question is not active.    Here's the scenario for a better understanding: The app is installed on the phone, but it's in the Destroy state, inside the app I set it to count from 10:00 p.m. only ... so even with the "not active" app I I want every day after 22hrs it tells how long the screen of the cellphone is not connected (how long the person remained without touching the cell phone) where I would capture this time and store for statistical purposes, in summary would be like an app task that it runs autonomously, almost like the Android OS, all this because I want to create an app that tries to guess how long the user has slept, so the question of the initial time, where it sets "normally I sleep by this schedule", then if the screen is off for more than 2hrs after that time the app will understand that the user is sleeping.

Please respond to me if this is possible so that I end up not injecting time into a project that can not be completed because it is too crazy rsrs. Thanks in advance for the community!

    
asked by anonymous 14.04.2018 / 22:00

1 answer

0

You can do this by using a service.

@ link Use AlarmManager to configure what time the service should start

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 22); // ou horário configurado pelo usuário
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
        new Intent(context, Sono.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pendingIntent );

Note: You would need to create a logic to finish the service later.

@ link In your Service OnStart () register to receive the ScreenOn and ScreenOff events

IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mScreenStateReceiver, screenStateFilter);

Remember to unregister in Service OnDestroy ()

unregisterReceiver(mScreenStateReceiver);

Every time the event is received, you can save the timestamp in a database and then extract the amount of time the screen left off (difference between each SCREEN_OFF and SCREEN_ON).

Remembering that Android can kill your service to free memory, then the count would be wrong. To reduce that change, you could create a persistent notification while the service is running.

    
14.04.2018 / 22:54