Today, we present you with an inquisitive tutorial on how to create a simple Android data analytics app in Python. The purpose of this Android app is to record and display the time you spend on the screen throughout the day using Python. To do this, it would need to continuously monitor screen usage, track the time, and display analytics.
Create a Simple Android Data Analytics App
To create a simple Android Data Analytics App using Python, we will use the Kivy library. Kivy is a cross-platform Python framework for developing graphical user interfaces (GUIs). It is also possible to use Python to develop Android apps using other libraries, such as SL4A (Scripting Layer for Android), but Kivy is the most popular and well-supported option.
Let’s now check out all the necessary steps that we need to get this simple Android data analytics app up and running.
Also Read: Create Your First Android App in Python
The Steps You Need to Follow
- Create a new Python file and import the necessary libraries. For instance, the following you would need:
kivy
: A cross-platform Python framework for developing graphical user interfaces (GUIs).logging
: A Python library for logging messages to a file or console.re
: A Python library for regular expression matching.Pandas
: A Python library for analyzing the data our app will record.- You can install these libraries using pip:
pip install kivy logging re pandas
Once you have installed the libraries, you can import them into your Python file and use them to analyze the screen time data. For example, you could use Pandas to create a data frame of the screen time data and then use Pandas to create a graph of the data.
Also Check: How to Convert Python Dictionary to DataFrame
- Define a class called ScreenTimeAnalyticsApp that inherits from the
Kivy
app class. - In the
__init__()
method of theAnalyticsApp
class, create the following attributes:ttl_scr_tm
: A label for displaying the total screen time.refresh_btn
: A button for refreshing the screen time data.clear_btn
: A button for clearing the screen time data.chart
: A chart for visualizing the screen time data.
- In the
build()
method of theAnalyticsApp
class, create a layout, and add the following widgets to it:- The
ttl_scr_tm
label. - The
refresh_btn
button. - The
clear_btn
button. - The
chart
widget.
- The
- Bind the
refresh_btn
andclear_btn
buttons to the corresponding methods of theAnalyticsApp
class. - Get the total screen time data for the day and update the
ttl_scr_tm
label andchart
widget with the data. - Return the layout from the
build()
method. - In the
main()
function, create an instance of theAnalyticsApp
class and run it.
Our Simple Android Data Analytics App Python Code
Let us now provide the Python code for our simple Android data analytics app that records your screen time and shows analytics.
import kivy from kivy.app import App from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.textinput import TextInput from kivy.uix.boxlayout import BoxLayout from kivy.uix.scrollview import ScrollView import logging import re import pandas as pd class Chart: def __init__(self): self.scr_tm = [] def update_data(self, ttl_scr_tm): self.scr_tm.append(ttl_scr_tm) class AnalyticsApp(App): def __init__(self): super().__init__() # Create the UI fields self.ttl_scr_tm = Label(text='Total screen time today:') self.refresh_btn = Button(text='Refresh') self.clear_btn = Button(text='Clear') self.chart = Chart() # Bind the UI fields to the respective methods self.refresh_button.bind(on_press=self.refresh_scr_tm) self.clear_button.bind(on_press=self.clear_scr_tm) # Get the total screen time data for the day ttl_scr_tm = self.get_ttl_scr_tm_for_day() # Analyze the screen time data self.analyze_scr_tm() # Update the UI elements with the data self.ttl_scr_tm.text = f'Average screen time today: {ttl_scr_tm} minutes' self.chart.update_data(ttl_scr_tm) def build(self): lt = BoxLayout(orientation='vertical') lt.add_widget(self.ttl_scr_tm) lt.add_widget(self.refresh_btn) lt.add_widget(self.clear_btn) lt.add_widget(self.chart) return lt def analyze_scr_tm(self): # Get the screen time data scr_tm = self.get_scr_tm() # Create a Pandas dataframe of the screen time data df = pd.DataFrame({'scr_tm': scr_tm}) # Calculate the average screen time avg_scr_tm = df['scr_tm'].mean() # Display the average screen time self.ttl_scr_tm.text = f'Average screen time today: {avg_scr_tm} minutes' def refresh_scr_tm(self, instance): # Get the total screen time data for the day ttl_scr_tm = self.get_ttl_scr_tm_for_day() # Update the UI fields with the data self.ttl_scr_tm.text = f'Total screen time today: {ttl_scr_tm} minutes' self.chart.update_data(ttl_scr_tm) def clear_scr_tm(self, instance): # Clear the screen time data # ... # Update the UI fields with the data self.ttl_scr_tm.text = 'Total screen time today: 0 minutes' self.chart.update_data(0) def get_ttl_scr_tm_for_day(self): # Get the Android log for the day log = logging.getLogger('AndroidLog') # Create a regular expression to match screen time events in the log scr_tm_evt_x = re.compile(r'Screen time (\d+) minutes') # Iterate over the log entries and find the total screen time for the day ttl_scr_tm = 0 for entry in log.manager.logger.log_entries: match = scr_tm_evt_x.match(entry) if match: scr_tm = int(match.group(1)) ttl_scr_tm += scr_tm return ttl_scr_tm # Run the app if __name__ == '__main__': AnalyticsApp().run()
A few notes about our simple Android data analytics app are as follows:
This code uses the Pandas library to calculate the average screen time and display it in a label. You can modify the code to analyze the screen time data in other ways, such as calculating the total screen time for the day, the screen time by app, or the screen time by day of the week.
This is a complete Python file for the screen time tracking app. The next thing is to save the file as AnalyticsApp.py
. Since we are almost done with the code part, let’s run it by following the steps in the next section.
Try It: How to Create an Android App in Android Studio
How to Run Our Simple Android Data Analytics App
The Python code we provided is a single file that you can use to create a real screen time tracking app. To run the app, you will need to have Kivy installed and you will need to grant the app READ_LOGS
permission. You can grant the permission by going to Settings > Apps > Screen Time Analytics App > Permissions > Read logs.
Once you have granted the permission, you can run the app by typing the following command in a terminal:
python AnalyticsApp.py
This will open the app on your Android device.
The app will display the total screen time for the day on a label. You can refresh the data by pressing the refresh button. You can also clear the screen time data by pressing the clear button.
The app is still under development, but it should give you a good starting point for creating your own screen time tracking app.
Must Read: Best Programming Language for Android
Conclusion
Finally, we finished a simple Android data analytics app using Python. The app will display the total screen time for the day on a label. You can modify the code to analyze the screen time data in other ways, such as calculating the total screen time by app, the screen time by day of the week, or the screen time trend over time.
Here are some suggestions for improvements:
- You could add a text input field to the app so that the user can specify the date range for the screen time analysis.
- You could add a chart to the app to visualize the screen time data.
- You could add the ability to export the screen time data to a CSV file.
Overall, the code for the Android screen time data analytics app is well-written and should work as expected.