Add it in your root build.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.developerdaya:GPSWorkManagerExample:Tag'
}
- Tracking Service: The app continuously tracks the user's GPS location and logs it in the background.
- Phone Unlock Detection: Tracks the phone unlock time using a broadcast receiver.
- Location Display: The app shows the user's last known location and the time it was updated.
- WorkManager: Periodically updates the location every 15 minutes.
- Data Persistence: Location data is stored in a Room database for persistent storage.
- Foreground Service: Used to ensure the app can track the location while running in the background.
- Permissions: The app requests multiple permissions, including fine location, coarse location, background location, and usage stats.
markdown The GPS Tracking App is an Android application that tracks device activities such as screen unlocks, GPS location updates, and app usage. It uses a combination of background services and receivers to continuously monitor and log data.
- GPS Location Tracking: Tracks the user's location in the background and saves it to a local database.
- Screen Unlock Detection: Logs the last time the device was unlocked using a broadcast receiver.
- Persistent Data Storage: Uses Room database for storing location logs.
- Background Service: Uses a Foreground service to ensure the app continues to function even in the background.
- WorkManager Integration: Schedules periodic location updates every 15 minutes using
WorkManager
. - User Interface: Displays the last known location and unlock time to the user.
The following permissions are required for the app to function:
ACCESS_FINE_LOCATION
andACCESS_COARSE_LOCATION
: To get precise and approximate location data.PACKAGE_USAGE_STATS
: To monitor app usage.FOREGROUND_SERVICE
: To run a background service.ACCESS_BACKGROUND_LOCATION
: To track location while the app is in the background.POST_NOTIFICATIONS
: To show notifications when the app is running in the foreground.
Make sure to request these permissions at runtime and handle denial cases properly.
- Enable Location Tracking: The user can enable or disable location tracking using a switch in the app.
- View Logs: The app shows the last known location and last unlock time on the main screen.
- Background Location: The app runs a background service to periodically update the location using
WorkManager
.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
- Location Tracking: The app tracks the user's GPS location every 5 seconds and logs it in the database.
- Phone Unlock Detection: Uses
PhoneUnlockReceiver
to capture unlock events and log the timestamp. - Database Interaction: Saves the location data into a Room database and retrieves it to display in the UI.
- LocationWorker: A worker class responsible for updating the location every 15 minutes in the background.
- ForegroundService: Runs a persistent foreground service that keeps the app active in the background for location tracking.
- SharedPreferenceUtil: Manages simple settings like whether tracking is enabled and the last known location.
- Room Database: For persistent data storage.
- WorkManager: For scheduling background tasks efficiently.
- FusedLocationProviderClient: For accessing location services.
- Kotlin Coroutines: For managing asynchronous background tasks.
-
Permission Handling: Ensure you're handling permission requests properly at runtime, especially for background location and usage stats.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE) }
-
BroadcastReceiver Registration: Make sure to register and unregister the
PhoneUnlockReceiver
properly inonResume()
andonPause()
methods, as you've already implemented. -
WorkManager for Background Tasks: Using
WorkManager
ensures the app performs periodic tasks in a battery-efficient way, so this is a good choice for background location tracking. -
UI Updates: Update the UI whenever new data is logged. For instance, when the phone unlock time is updated, reflect the change on the main screen.
-
Data Persistence: Using
Room
is a great approach for persisting data. Ensure you're handling database interactions off the main thread, ideally with Kotlin coroutines.