A service that runs every day in the background on android

Asked

Viewed 100 times

0

Good afternoon ! Perhaps my question does not fit the criteria perfectly because it is another question whether "it is possible or not possible". Here is the problem: I want to create an app in the following scenario: it will mark how long the mobile screen is turned off after a certain time even if the app in question is not active. Follow scenario for better understanding: The app is installed on mobile, but is in the Destroy state, within the app I set for it to count from 10 hrs of the night only... so even with the app "not active" I want every day, after the 22hrs he tells how long the screen of the mobile phone is not turned on (how long the person was left without touching the mobile phone) where I would capture this time and store for statistical purposes, in short it would be like a task of the app that runs autonmamente, almost like the Android OS, all this because I want to create an app that tries to guess how much time the user has slept, so the question of the initial time, where it configures "normally I sleep for this time"then if the screen is off for more than 2hrs after this time the app will understand that the user is sleeping.

Please answer me if this is possible so that I end up not injecting time into a project that cannot be completed by being too crazy rsrs. Thanks to the community!

1 answer

0


You can do it using a service.

@https://stackoverflow.com/a/6841929/1615055 Use Alarmmanager to set 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.

@https://stackoverflow.com/a/9478013/1615055 In your Onstart Service() register to receive 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 unsubscribe from Ondestroy Service()

unregisterReceiver(mScreenStateReceiver);

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

Remembering that Android can kill your service to release memory, then the count would be wrong. To lessen the change from this happening, you could create a persistent notification while the service was running.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.