How do I track app usage time in android? - android

I have been searched for a long times, but I cannot solve this.
I am developing app usage appplication, and I want to make user can restrict app usage time.
lets say user want to restrict Youtube app usage time for 10 min.
1.
How can I make notification or toaster when Youtube app usage reach 10 min since user click button?(I know event handling)
can I make observer even my app goes backgroud? or even after detroyed?
Base on my search registerAppUsageObserver looks like what I want to, but I cannot use it since its #hide
public void registerAppUsageObserver(int observerId, #NonNull String[] observedEntities,
long timeLimit, #NonNull TimeUnit timeUnit, #NonNull PendingIntent callbackIntent)
I can try to use it by reflection but I think its not recommended.
I got one more good source link but I don't know how to use it.
please help me. thank you.

I've done a fair bit of work in this area. I'd like to direct you to my smartCBT GitHub that reviews in a given period of time what a person has been using their apps for (https://github.com/kris-geyer/smartCBT; in particular, check out https://github.com/kris-geyer/smartCBT/blob/master/app/src/main/java/kg/own/smartcbt/ViewModel/RetrieveUsageRecords.java).
First off, I wouldn't trust the usage stats manager. It's been very unreliable in the past for me. I think what you want to assess usage is usage events (https://developer.android.com/reference/android/app/usage/UsageEvents.Event). These give you a much more fine-grained account of android app usage which seem much more accurate. You will need to get special permission for this. I've actually, empirically tested the accuracy before and it is very high.
For the overall structure of what your app needs I think you want a foreground service that runs in the background routinely logging what the person is using their phone for. This database is only updated when apps are changed, so handling this might be a little tricky but you'll manage.
When the app has identified that the allotted amount of usage time for a particular app is up, then it can direct the user back to your app by launching a new activity from the foreground service. I'm not sure if this is still possible, the android permissions seem to change so quickly. What would be really cool is if you can get the app to play videos of the user telling themselves that they've used their phone too much and be productive.
Best of luck. :)

Related

Is it possible to create a background service that never of the never dies on Android > 10?

I know that similar questions have been posted in the past and the most current solution I have found is to use a JobScheduler + wakelock + Foreground Service as explained for example in this excellent article by Roberto Huertas (https://robertohuertas.com/2019/06/29/android_foreground_services/).
However my doubt is to know if there is a limit for this method. Does it really work that well? What if the App stays in the background for days or even weeks, will it still work?
If the answer to these last questions is no, is it possible to keep a background service on Android > 10 that can keep running for days without stopping?
EDIT 1:
I'm trying to create a real time GPS tracking app (In this case I'm using Firebase). The company and the users who use it, give their consent to be tracked all day long during their activity. This tracking can be stopped if the user disables the option inside the APP.
I have managed to keep the service running in the background using various techniques, but after a few hours Android kills it.
No, it's not. It's actually less possible than ever. Background services are now limited to 2 minutes after you exit the foreground. Foreground services will be kept around for a while, but they won't stick around forever.
The correct answer on Android is to find a way NOT to need a service running at all times. This is almost always possible, but methods differ depending on what you actually need to do, which you haven't given us any info on.

Foreground or Background service to track user's location?

I'm developing a location aware app. This app will start tracking users when they are in their workday. These are the requirements:
The service should run on a regular basis (every 30 or 45 min).
It won't matter if the service does not trigger at the same basis everytime.
The service needs to upload data to our firestore db.
I doesn't need to be triggered on specific conditions (data is on, phone is charging, etc...)
I need this to run even if the user restarts his phone.
We may need to track the distance traveled by the user. (This is not a requirement per se, but it may be a feature someday)
I know there are a lot of ways to achieve this, but I have not decided which is the option that best fits my scenario. This is what I've thought so far:
Foreground service combined with BroadcastReciever in case the phone is rebooted
Background service using the new jetpack's Workmanager.
Which will the best solution? Do you think there is a better solution than those?
Thanks!
Was thinking create a GPS location tracker so when they are in work premise as well as outside it kinda shows.
Then consider adding the number 5 of the above. Like you said there could be mire awesome solutions than these so lets wait for options.

Content serving in android

Though as a beginner, I am trying to develop an android app which is story based, i will like to know the best way to serve content to the user, i mean like a continuous update of content, just like updating news by the hour. As users will install just once, how will they get the latest content of my news or story based app.
I have access to domain names and hosting if it requires uploading such content through a domain.
from your experience, what is the best method to achieve this? I humbly await a response, thanks
So given the clarification in the comments this is the answer:
The best way is PUSHING the content to the user's device.
Generally speaking, the two ways for a new content to reach an app are :
1.polling your server (or any third-party server) for new data every,say, 20 minutes. The disadvantage of this method is that it drains the battery. Every time that the phone connects to the internet, the radio in that phone stays on (or in a standby mode) for something like 2 minutes. Those modes (on and standby) drain the battery. Another problem is that it does use data needlessly and in some countries cellular data is expensive (Canada for example).
This could be a solution if the data changes very very frequently (for example a stock's price can change many many times a day). But generally speaking method 2 is the preferred method..
2.Pushing the content to the user's phone.
Your server will send a message to the device once new data that you want to send is there (and you could also put that data in the payload of the message if it's not too much).
This means that the phone will connect only when some new data is available.
Saves battery life,and gets the information as soon as it is available!
I recommend using GCM (Google Cloud Messaging) for this purpose which is free, and simple to use. If you have no idea how to do that in Android (which is likely since you said that you are a beginner), it is explained really well in Udacity's
Advanced Android App Development. It is a free course by Udacity and Google, but the section about GCM is only about 15 minutes long.
If you know how to implement a server but don't know how to use GCM in your server (and you don't find Google's documentation helpful) do let me know..
If you don't know how to implement a server...well then it's something you will have to learn in order to get your content to your users as that's the best way.
I hope this helps! :)

Do something at a certain time of day in an android app

I'm writing a reminder app for Android where users can create custom notifications that will appear at a time of the day that they specify. I realize that there is another similar question, but the only answer was to use the AlarmManager, which, according to the documentation, resets when the device is rebooted. I want the user to be able to set a notification to appear at a certain time on a certain date, meaning that it should retain the information through a reboot.
Ultimately though, the app needs to be notified that it needs to do something.
Eventually, I would also like to have the same behavior when the device reaches a certain location, if you have any thoughts on that. :)
Bear with me, I'm a bit of a novice programmer (I'm 17).
Thanks in advance.
The commentors are correct: you will need to use an AlarmManager to create your alarm and you will need a boot receiver to handle resetting your alarm after a reboot.
GeoLocation and GeoFences are pretty easy. I have an example app here: https://github.com/androidfu/GeofenceExample
You're not going to care about the mock location bits in that example unless you wish to test entering and exiting your target location, but the rest of the code should work for what you need.
Also, GeoFences do not persist a reboot either so it'll be good for you to get familiar with your on-boot receiver ;) You'll want to re-add your GeoFence after a reboot too.

Battery notification in every application that is running

Is it possible to have a battery notification in every application that is running?
For example: If I'm going to run an application like Angry Birds, is it possible to have some sort of a notification that will tell me if there is enough time to continue playing it?
This is one of the objectives in our thesis.
If I'm going to run an application like Angry Birds, is it possible to have some sort of a notification that will tell me if there is enough time to continue playing it?
You can play Angry Birds for a second, a minute, an hour, a day, a week, a month, etc. There is no defined "enough time to continue playing it".
Moreover, it is impossible to state definitively how much power a process will run in the future.
Now, you could create some code that attempts to measure historical power consumption of an application (and how the user uses it, which may vary from person to person). You could use that information to make some educated guesses about how long the user could keep using that app. And you could find a way to present that information to the user.
All of that will require, at minimum, custom firmware. It is also very possible that it will require custom hardware that has fine-grained power consumption data collection, such as the Qualcomm MDP.

Categories

Resources